Synchronizing threads
As you saw in the previous examples, the threading
module has many advantages over its predecessor, the thread
module, in terms of functionality and high-level API calls. Even though some recommend that experienced Python developers know how to implement multithreaded applications using both of these modules, you will most likely be using the threading
module to work with threads in Python. In this section, we will look at using the threading
module in thread synchronization.
The concept of thread synchronization
Before we jump into an actual Python example, let's explore the concept of synchronization in computer science. As you saw in previous chapters, sometimes, it is undesirable to have all portions of a program execute in a parallel manner. In fact, in most contemporary concurrent programs, there are sequential portions and concurrent portions of the code; furthermore, even inside of a concurrent portion, some form of coordination between different threads/processes...