CST 334 Week 5 Journal Entry


Concurrency

This week, we learned about the importance of threads in a process. A thread is a point of execution for a single running process. When a program is running, it can have more than one thread running at a time. Each thread has its own stack to store local variables and function calls, but resources such as code and heap can be shared from the program. Threads are like separate processes, except they share the same address space. When we switch between threads, thread control blocks are used to store and restore the state of the thread. This is a quick action as the threads share the same address space. Threads can help run tasks parallel to each other on different CPUs or run concurrently on the same CPU. 

When more than one thread runs concurrently, it is important to take some precautions to avoid a race condition. A race condition occurs when threads in a program access critical sections of code concurrently. This can lead to the program being indeterminate, which is when the program produces different results for each run. The critical section is where shared resources are accessed, so it is important that a thread that accesses this area of code finishes it before another thread accesses it. Failure to do so will result in indeterminate results. 

We can prevent race conditions by making sure a critical section is accessed by a thread one at a time (mutually exclusive), each thread has its own chance to run the critical section, and the time overhead is maintained. This is done by using locks, conditional variables, and synchronization primitives offered by the hardware. Locks allow threads to be owners of a critical section until completion and prevent other threads from interfering until the lock is released. Conditional variables allow threads to wait for the critical section to be released. When it is released, a conditional variable gives a signal to another thread to give it access to the critical section. Using a combination of locks and conditional variables, we can develop different types of locks, such as the Spin Lock with Test and Set and the Ticket Lock. These locks can also be applied to data structures such as linked lists and hash maps. 

Comments

Popular posts from this blog

CST 300 - Week 8

CST 300 - Week 5

CST 300 - Week 4