Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutex for simple data types

Tags:

c++

mutex

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.

like image 474
Q-bertsuit Avatar asked Sep 27 '22 03:09

Q-bertsuit


2 Answers

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.

like image 189
Cory Kramer Avatar answered Oct 06 '22 19:10

Cory Kramer


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.

like image 35
Yves Daoust Avatar answered Oct 06 '22 19:10

Yves Daoust