Locking
The use of atomic operations is a good solution for synchronizing access to a single variable. We often need to synchronize more complex sections of code, such as algorithms that rely on the value of multiple variables or functions that touch hardware. To remove the possibility of race conditions from these more complex areas of code, we rely on mutual exclusion; that is, any other thread that wishes to execute code which could interfere with the result of our operation is blocked until the operation is complete. The act of obtaining exclusive access is referred to as “acquiring a lock.”
The basic idea behind locking is that any code that accesses a shared resource, such as the instance variables of a driver, will first acquire a lock ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access