问题
《C++并发编程实战》第二版中的代码2.6,作者实现了如下代码
#include <thread>
#include <utility>
class scoped_thread
{
std::thread t;
public:
explicit scoped_thread(std::thread t_):
t(std::move(t_))
{
if(!t.joinable())
throw std::logic_error("No thread");
}
~scoped_thread()
{
t.join();
}
scoped_thread(scoped_thread const&)=delete;
scoped_thread& operator=(scoped_thread const&)=delete;
};
void do_something(int& i)
{
++i;
}
struct func
{
int& i;
func(int& i_):i(i_){}
void operator()()
{
for(unsigned j=0;j<1000000;++j)
{
do_something(i);
}
}
};
void do_something_in_current_thread()
{}
void f()
{
int some_local_state;
scoped_thread t(std::thread(func(some_local_state)));
do_something_in_current_thread();
}
int main()
{
f();
}
这个代码不管在VS中还是g++中都无法运行,虽然编译和链接没报错,但是打断点发现scoped_thread
的构造函数和线程函数都没有运行。
解决方案
void f()
{
int some_local_state;
scoped_thread t(std::thread((func(some_local_state))));
do_something_in_current_thread();
}
在func前后再加一对括号即可
问题原因
测试了很多次,我个人理解,应该是跟C++'s most vexing parse
有关。它的大概意思就是:
C++ 标准规定把这样的 statement 视作函数声明。
T1 name1(T2(name2));
根据 C++ 标准,此时不把 T2(name2) 视为「a function style cast」,而将其视为 T2 name2,这样整个语句就变成:T1 name1(T2 name2);,显然这是个函数声明。
类似地,T1 name1(T2(name2), T3(name3))
;被视作 T1 name1(T2 name2, T3 name3)
。
如果增加这么一个函数,
std::thread func(int& i)
{
return std::thread(do_something_in_current_thread);
}
设置断点,会发现scoped_thread走的就是新加的这个函数
scoped_thread t(std::thread(func(some_local_state)));
也就是说,上面这行代码,本意是想创建一个临时的std::thread
对象,实际上,却被C++解释成了,调用std::thread func(some_local_state)
函数,这个函数返回一个std::thread
对象,接收一个int
类型的参数。
加括号表示是一个表达式,对其可以求值。而不加括号被编译器理解成一个函数,所以要加括号。
Fctor() :可以理解成函数名字为Fctor的函数。
(Fctor()): 作为表达式。
这个问题实际上作者在2.1.1节的时候也有讲过,不知道为什么这里又犯了这个错。