Could you tell mw what is the problem with the below boost::thread program
#include<iostream>
#include<boost/thread/thread.hpp>
boost::mutex mutex;
class A
{
public:
A() : a(0) {}
void operator()()
{
boost::mutex::scoped_lock lock(mutex);
}
private:
int a;
};
int main()
{
boost::thread thr1(A());
boost::thread thr2(A());
thr1.join();
thr2.join();
}
I get the error message:
error: request for member ‘join’ in ‘thr1’, which is of non-class type ‘boost::thread()(A ()())’
BoostThread2.cpp:30: error: request for member ‘join’ in ‘thr2’, which is of non-class type ‘boost::thread ()(A ()())’
You have stumbled on something wonderfully known as the most vexing parse. The quickest way to fix that is to add an extra set of parentheses:
You can also introduce a temporary:
In the most vexing parse, what you think is generating a temporary is parsed as if it’s a function taking no parameters. It then treats thr1 as a prototype to a function that takes a single parameter (which is the function mentioned previously) and returning a boost::thread.