I'm pretty new to concurrency, and I'm having trouble deciding on how to use mutexes. At the moment they are sprinkled all over my code where two threads interact. Would this use of mutexes be appropriate?
class Foo
{
public:
void SetMember(int n) { AcquireMutex(..); n_ = n; ReleaseMutex(...);}
private:
Thread()
{
while(1)
{
AcquireMutex(..);
// Do something with n_
ReleaseMutex(...);
}
}
};
I have quite a few data members that can be read and set from the outside by a different thread, and I'm finding it to be a headache to keep track of all the acquiring and releasing of mutexes.
Mutation of primitive types is not guaranteed to be thread safe, or more specifically atomic. In fact, if you look in <atomic>
you will notice that there are several specializations, including std::atomic_int
.
From cppreference
Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.
To specifically answer your question about the use of mutexes, yes your use of mutexes is fine in your example. In general, you want to hold a mutex for as short as possible. In other words, if you have a function that does a lot of work, only lock the mutex around the un-threadsafe code, then unlock it as soon as the code is threadsafe afterwards.
This approach of mutual exclusion is pretty naive and it will not avoid the problems of deadlock and other issues with concurrency.
You must explicitly identify the places in your code where simultaneous accesses by two processes would cause a problem and protect them in a critical section. And you must make sure that you will not let two processes start an operation that they cannot complete because of another process.
At other places, there could be no need for protection and activating a mutex would be overkill.
Very often, atomicity is required not for a single variable but for a set of coupled variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With