Journal Week 5
The most interesting piece was about the magic of concurrency. Rather than just having one path of execution, threading allows for multiple paths of execution to happen at once. But then a problem can happen when multiple threads reach a part of the code where resources or variables are shared amongst different processes. It’s okay if they are just reading the data, but if information is going to be read and overwritten it becomes a problem. If two threads, try to access and manipulate data at the same time then it may cause problems as the variable or resources are no longer the indented values. This dilemma where resources are being read and overwritten at the same time is called a race condition.
With race conditions causing unpredictable results, mutexes come to the rescue to help control the rate at which threads access critical sections Mutex is short for mutual exclusion. Mutexes serve as a “lock” around critical parts of code. This lock allows only one thread to access it at time. Once a thread is done with all its processing in a mutex locked part of the code, then it unlocks it and makes it available to a different thread to use.
Locks can be used in data structures to make them thread safe. Being thread safe means that it is allowed to have multiple threads to access the resource(s) without causing unpredictable behavior. By making a structure thread safe, it can increase effect versus only letting one thread access a resource at any given moment.
Comments
Post a Comment