CST-334 Week 5

 This week, I learned the benefits of concurrency, the C pthreads API, and how locks are used to protect programs from race conditions. Concurrency is beneficial as it is a useful programming abstracting, increases responsiveness, and it can leverage multicore machines and GPUs. On the other hand, concurrency can introduce bugs related to concurrency, and it can cause programs to be non-deterministic. A program is not deterministic when the output of the program is not expected and changed each time it is ran. This is commonly caused by race conditions. A race condition arises when multiple threads of execution enter the critical section (a shared resource) at roughly the same time. The threads will attempt to update the shared data structure and as the schedular will be making the decision on which thread will be run, the output if the program will become undesirable. Programs can avoid these problems by having threads use some kind of mutual exclusion primitive, which will guarantee that only a single thread enters a critical section. Locks can be used in programs to be mutually exclusive. Before entering a critical section, place a lock and then after, unlock that section. This will only allow one thread at a time to access a shared resource, avoiding any chances of introducing race conditions. This program will then be known as thread safe. I thought this term was interesting to hear as I have heard of the term before, but I wasn't sure what it really meant. After learning about thread safe programs, it made me realize that some of the programs I have written may not be safe to use with threads.

To create a thread in a C program, we can use pthreads_create. Pthreads_create takes four parameters. One, a pointer to a thread. Two, the thread attributes. Three, the function to start running. And four, the argument to pass into the running function. After starting the thread, we will need to wait for the thread to finish in our main function, or there is a risk of the program finishing before our thread finishes executing. To achieve this, we can use Pthread_join. Pthread_join takes two parameters. A pthread structure and a pointer to the expected return value.

Comments

Popular posts from this blog

Week 4

Week 5

Week 6