In addition to the tips and guidelines that were provided earlier (refer to the Locking Guidelines section), think upon this as well:
- How many locks should one use?
- With many lock instances, how to know which lock variable to use and when?
- Test whether a mutex is locked or not.
Let's take these points up one by one.
In small applications (like the kind shown here), perhaps using just a single lock to protect critical sections is enough; it has the advantage of keeping things simple (which is a big deal). However, in a large project, using just one lock to perform locking on every critical section one might encounter has the potential to become a major performance breaker! Think about why exactly this is: ...