MultiThreading
|
Assume there are some global data, which may be repeatedly accessed by different threads. As long as all threads are only reading the data there is no need for special synchronization. But if at least one thread may change the data while others are trying to read them you should use a mutex for controlling access:
Within each thread you have to acquire the mutex before accesssing data. When you are ready with processing data you should release the mutex to give other threads a chance to access data.
As long as the mutex is locked by some other thread the code will be blocked within the constructor call of myLock. When the mutex gets available the code sequence will proceed.
Instead of using a mutex directly you should always use a guard object for calling lock() and unlock() methods of the mutex. Especially in case of exceptions the destructor of the guard object will ensure that the mutex is unlocked.
The sample code above uses a recursive mutex. Thus a thread already having acquired the mutex can repeatedly lock the mutex (e.g. by calling some helper functions which itself care about locking). You can use a more strict programming paradigm allowing only a single lock of a mutex regardless whether it is the same thread or another thread wanting to access the data. For this paradigm you should use std::mutex instead of std::recursive_mutex.
It may be necessary to repeatedly access shared data and in between give other threads a chance for data access:
Sometimes it is useful to process data only if they are available immediately or within a short time period.
First you have to use a special kind of mutex:
Within the thread function trying to access global data you may be prepared for not getting access:
A typical deadlock situation may arise if different threads try to lock resources A and B in different orders. One thread may have locked resource A and waits until resource B gets free. The other thread may have locked resource B and waits until resource A gets free.
A solution to avoid this situation is to lock only both resources or none of them: