How to use a std::mutex in a class context

后端 未结 2 1047
生来不讨喜
生来不讨喜 2021-02-14 20:18

i\'m having some trouble using a C++11 std::mutex

in my class I have a variable called semaphore of type std::mutex.

So I positioned my semaphore.lock() and sema

2条回答
  •  后悔当初
    2021-02-14 20:49

    Declare your mutex dynamically and not statically.

    At your Database class header declare the mutex as a pointer, like that:

    mutex * semaphore;
    

    and at your constructor initialize the mutex calling his constructor:

    semaphore = new std::mutex();
    

提交回复
热议问题