Reputation: 483
Passing a mutex reference to a thread causes compile errors. Why is it not possible (I have multiple threads using the same shared variable), and how do I fix it?
#include<iostream>
#include<thread>
#include<mutex>
void myf(std::mutex& mtx)
{
while(true)
{
// lock
// do something
// unlock
}
}
int main(int argc, char** argv)
{
std::mutex mtx;
std::thread t(myf, mtx);
t.join();
return 0;
}
Upvotes: 16
Views: 8679