-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
The C++ Programmer's Mindset
By :
Any programmer will tell you that one of the first problems one might encounter in a multithreaded program is data races. This is where one thread reads a value that is modified by another thread. Since the order of thread execution is non-deterministic, one cannot know in which order the read and write occur. Thus, the value that is accessed is unknowable. Most programming languages provide mechanisms for preventing data races, such as mutual exclusion (mutex) locks.
Mutex locks are a very big hammer for preventing data races. The idea is that one thread acquires the lock and then performs work that modifies the data that the lock protects. Each new thread that tries to acquire the lock now blocks (it doesn’t proceed any further, yielding to the operating system to allow other processes/threads to run) until the original thread releases the lock. This obviously means that your program cannot do as much work; the purpose of...