c++11 std::mutex compiler error in Visual Studio 2

2019-02-18 13:36发布

问题:

This a quest about deadlock in C++11 standard.

In the sec3.2.4 of C++ Concurrency in Action, there is an example for preventing multithreads from deadlock. For guys without this book, in addition, there is an another almost similar example you can refer to: http://en.cppreference.com/w/cpp/thread/lock_tag

The problem I encountered is that the codes of both codes arise compiler-errors in Visual Studio 2012. The error message is:

'std::mutex::mutex': cannot access private member declared in class 'std::mutex'

This problem also happens in the following simpler code than in cppreference.com:

struct bank_account {
    std::mutex m;
};
void transfer(bank_account &from, bank_account &to)
{
    std::lock(from.m, to.m);
}
int _tmain(int argc, _TCHAR* argv[])
{
    bank_account my_account;
    bank_account your_account;

    std::thread t1(transfer, my_account, your_account);  // compiler-error!

    std::system("pause");
    return 0;
}

Any idea to solve this problem in Visual Studio 2012?

回答1:

mutexes are not copyable or assignable, and the std::thread constructor is attempting to make a copy. You can circumvent this by using an std::reference_wrapper via std::ref:

std::thread t1(transfer, std::ref(my_account), std::ref(your_account));

alternatively, you can pass temporary bank_accounts:

std::thread t1(transfer, bank_account(), bank_account());

This will most likely result in the bank_accounts being "moved" rather than copied, although it is also possible that the copy will be avoided via copy elision.



回答2:

You are making copy of my_account and your_account to std::thread, but std::mutex is neither copyable nor movable.

Try pass by reference:

 std::thread t1(transfer, std::ref(my_account), std::ref(your_account));


回答3:

From this reference:

The mutex class is non-copyable.

A mutex can't be copied or moved.