我正在使用带有 c++11 标志的 g++ 4.7。在这个演示中:
#include <iostream>
#include <thread>
class do_work
{
public:
void operator()()
{
std::cout << "Doing work..." << std::endl;
}
};
void foo()
{
}
int main()
{
// Does not work
std::thread t(do_work);
t.join(); // error: request for member ‘join’ in ‘t’, which is of non-class type ‘std::thread(do_work)’
// Works
std::thread t2(foo);
t2.join();
return 0;
}
我可以在使用函数作为其构造函数参数创建的线程上成功调用 join(),但我不能在使用仿函数作为其构造函数参数创建的线程上调用 join()(参见错误内联)。谁能解释一下?