C++ thread不运行

《C++并发编程实战》第二版中有如下代码,我在VS2015和其他版本中都没有运行起来。

#include <thread>
#include <utility>
#include <stdexcept>

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();
}

为什么do_something函数没有运行?乃至scoped_thread的构造函数都没有执行?

void thread_func(int& var)
{
    std::cout << var << std::endl;
}

void f()
{
    int some_local_state = 10;

    //std::thread t1(func(some_local_state));
    //std::thread a(thread_func,std::ref(some_local_state));
    //functionTest(std::move(a));
    scoped_thread t(std::thread(thread_func, std::ref(some_local_state)));
    //scoped_thread t(std::thread(func(some_local_state)));

    do_something_in_current_thread();
}

这样就能运行了,应该是仿函数的问题,但是具体是什么原因不清楚。

你遇到的问题是C++语言中Most Vexing Parse造成的。

对于你代码中49行的语句,即下面这条语句

scoped_thread t(std::thread(func(some_local_state)));

C++编译器实际上是把它当成了函数声明解析了,即这条语句声明了一个函数t,其返回值是scoped_thread,参数func是一个类型为std::thread (*)(int)的函数指针。而不是如你想象的那样定义一个局部变量。因此也就没有创建线程,执行func函数对象。

解决这个问题的方法也很简单,只要让上面语句变成不是函数声明形式即可,比如
方法一:把t的参数用额外一对()括起来

scoped_thread t((std::thread(func(some_local_state))));

方法二:使用{} uniform initialization

scoped_thread t{std::thread(func(some_local_state))};

关于Most Vexing Parse,你可以参考维基上的介绍。
https://en.wikipedia.org/wiki/Most_vexing_parse

有些编译器(除了g++外)会对这种情况给出警告,比如用clang++编译器编译上面代码给出下面警告

$ clang++ -Wall main.cpp -lpthread
main.cpp:50:20: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
    scoped_thread t(std::thread(func(some_local_state)));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:50:21: note: add a pair of parentheses to declare a variable
    scoped_thread t(std::thread(func(some_local_state)));
                    ^
                    (                                  )

用MSVC编译器编译上面代码给出下面警告

main.cpp(50,19): warning C4930: 'scoped_thread t(std::thread (__cdecl *)(func))': prototyped function not called (was a variable