Mutex for simple data types

痴心易碎 提交于 2019-12-10 14:47:09

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31808927/mutex-for-simple-data-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!