Speaking of threads...
Throughout this entire chapter, we've been using the word "thread" without ever defining exactly what we mean by it; and you've probably noticed that many of our multithreaded code examples have used the class type std::thread
and the namespace std::this_thread
without much explanation. We've been focusing on how to synchronize behavior between different threads of execution, but so far we have glossed over exactly who is doing the executing!
To put it another way: When execution reaches the expression mtx.lock()
, where mtx
is a locked mutex, the semantics of std::mutex
say that the current thread of execution should block and wait. While that thread is blocked, what is happening? Our C++ program is still "in charge" of what's going on, but clearly this particular C++ code is no longer executing; so who is executing? The answer is: another thread. We specify the existence of other threads, and the code we want them to execute, by using the standard library class std...