使用类作为参数创建线程并使用类构造函数传递数据时出错

时间:2015-02-15 22:10:48

标签: c++ multithreading c++11 stdthread

我正在将某些代码现代化为c ++ 11'标准'(std :: thread等),我正在尝试使用std :: thread生成一个线程并将一个类作为参数传递但是我出于某种原因无法让它工作..

班级定义:

Class Listener
{
...
private:
...
    Class Worker
    {
    ...
    public:
    Worker(struct sockaddr_in server_sockaddr, struct sockaddr_in client_sockaddr, socklen_t blah, int blah2)
    { 
        ....
    }
    };
...
int mah_function_in_class_Listener()
{
    ...
    thread thread_id=thread(Worker(server_sockaddr, client_sockaddr, client_addr_size, accpt));
    ...
}
};

当我尝试编译时,我得到:

In file included from /usr/include/c++/4.8/thread:39:0,
                 from ./tcp_server_singlethreaded.cpp:26:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<Listener::Worker()>’:
/usr/include/c++/4.8/thread:137:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = Listener::Worker; _Args = {}]’
./tcp_server_singlethreaded.cpp:199:106:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<Listener::Worker()>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<Listener::Worker()>’
         _M_invoke(_Index_tuple<_Indices...>)
         ^

据我所知,这是因为正在返回std :: thread对象,而不是std :: thread(??:S)类型的对象thread_id,我认为复制初始化或使用{}会解决这,但显然这里还有其他问题..

感谢您看一下 - 希望有人能够帮助您解决此问题。

非常感谢!

修改

我忘了提一个小细节.. g ++ / gcc 4.8.2(4.8.2-19 ubuntu) - 我不认为这是一个gcc概率(更可能是pebkac问题)..

0 个答案:

没有答案