Friday, 15 August 2014

c++ - c++11 std::mutex compiler error in Visual Studio 2012 -



c++ - c++11 std::mutex compiler error in Visual Studio 2012 -

this quest deadlock in c++11 standard.

in sec3.2.4 of c++ concurrency in action, there illustration preventing multithreads deadlock. guys without book, in addition, there similar illustration can refer to: http://en.cppreference.com/w/cpp/thread/lock_tag

the problem encountered codes of both codes arise compiler-errors in visual studio 2012. error message is:

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

this problem happens in next simpler code 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"); homecoming 0; }

any thought solve problem in visual studio 2012?

mutexes not copyable or assignable, , std::thread constructor attempting create copy. can circumvent using std::reference_wrapper via std::ref:

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

alternatively, can pass temporary bank_accounts:

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

this result in bank_accounts beingness "moved" rather copied, although possible re-create avoided via copy elision.

c++ multithreading c++11 locking mutex

No comments:

Post a Comment