CST 334 - Week 5
This week was about concurrency. More specifically about multithreaded programs. A thread is an abstraction of a single process. They are similar to a process except that threads share the same address space as the process that created them. Threads are also indeterminate which means they will run at a different time producing different results every time the program is run. This issue can be solved using locks which will prevent threads that are running at random times to wait until the desired thread completes what the program intends. Threads are very useful because it allows a process to handle multiple lightweight tasks at the same time.
We also had some hands-on work with threads in the lab for this week. This lab created 10 threads that would pass an incrementation counter to another function that would output text to the screen. When running this program a bug would occur that would sometimes show the same number for the increment even though should have been updated in the loop. This was showing an example of multiple threads that by nature will complete at random different times that the CPU schedules. The bug was a result of certain threads finishing before or after the incrementation loop updated the increment resulting in the same value being passed to the new thread. Creating a different counter and passing the address of that variable to the pthread_create() function allowed for the correct amount of iteration to be shown.
Comments
Post a Comment