std :: thread,start线程在`this'(从类本身) [英] std::thread, start thread on `this' (from within the class itself)
问题描述
我试图创建一个类,它启动其成员方法之一的线程实例。
当我在主要做:
I'm trying to create a class which starts a thread-instance of a one of its member methods. When I do in main:
test myinstance;
std::thread mythread(myinstance);
然后编译。但使用以下结构:
then things compile. But using the following construction:
#include <stdio.h>
#include <unistd.h>
#include <thread>
class test
{
std::thread *pt;
public:
test()
{
pt = new std::thread(this);
}
void operator()() const
{
printf("thread start\n");
sleep(5);
printf("thread end\n");
}
};
int main(int arg, char *argv[])
{
test ptest;
sleep(10);
return 0;
}
我收到以下错误:
folkert @ here:〜$ g ++ -std = c ++ 0x test.cpp在
中包含的文件中/usr/include/c++/4.6/thread:39: 0,
从test.cpp:3:/usr/include/c++/4.6/functional:在成员函数'void std :: _ Bind_result< _Result,_Functor(_Bound_args
...)> :: __call(std :: tuple< _Args ...>&&&&&&&std :: _ Index_tuple< _Indexes
...>,typename std :: _ Bind_result< _Result,_Functor(_Bound_args
... )> :: __ enable_if_void< _Res> :: type)[with _Res = void,_Args = {},
int ..._ Indexes = {},_Result = void,_Functor = test *,_Bound_args =
{},typename std :: _ Bind_result< _Result,_Functor(_Bound_args
...)> :: __ enable_if_void< _Res> :: type = int]':
/usr/include/c++/4.6 / functional:1378:24:从
实例化的std :: _ Bind_result< _Result,_Functor(_Bound_args ...)> :: result_type
std :: _ Bind_result< _Result,_Functor(_Bound_args
...)> :: operator()(_ Args&& ...)[with _Args = {},_Result = void,
_Functor = test *,_Bound_args = {},std :: _Bind_result< _Result,_Functor(_Bound_args ...)> :: result_type = void] '/usr/include/c++/4.6/thread:117:13:实例化为'void
std :: thread :: _Impl< _Callable> :: _ M_run()[with _Callable =
std :: _bind_result]'test.cpp:28:1:从
实例化这里/usr/include/c++/4.6/functional:1287:4:错误:
'((std :: _ Bind_result *)this) - > std :: _ Bind_result :: _ M_f'不能用作函数
folkert@here:~$ g++ -std=c++0x test.cpp In file included from /usr/include/c++/4.6/thread:39:0, from test.cpp:3: /usr/include/c++/4.6/functional: In member function 'void std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__call(std::tuple<_Args ...>&&, std::_Index_tuple<_Indexes ...>, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type) [with _Res = void, _Args = {}, int ..._Indexes = {}, _Result = void, _Functor = test*, _Bound_args = {}, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type = int]': /usr/include/c++/4.6/functional:1378:24: instantiated from 'std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type std::_Bind_result<_Result, _Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {}, _Result = void, _Functor = test*, _Bound_args = {}, std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type = void]' /usr/include/c++/4.6/thread:117:13: instantiated from 'void std::thread::_Impl<_Callable>::_M_run() [with _Callable = std::_Bind_result]' test.cpp:28:1: instantiated from here /usr/include/c++/4.6/functional:1287:4: error: '((std::_Bind_result*)this)->std::_Bind_result::_M_f' cannot be used as a function
所以我的猜测是,它不会工作这种方式。
现在的问题是:我怎么能使用std :: thread让一个类启动一个自己方法的线程?
So my guess is that it won't work this way. My question now is: how can I, using std::thread, let a class start a thread of one of its own methods?
推荐答案
std :: thread
的构造函数之一如下所示:
One of std::thread
s constructors looks like this:
template<typename Callable>
explicit thread(Callable func);
这需要您传递可调用可以使用 operator()
调用。您传递给 std :: thread
的是不可调用的。
This requires you to pass something that is callable, that means it can be invoked with operator()
. What you're passing to your std::thread
is not callable.
您不能调用 this
。 此
是指向当前对象的指针,不可调用。
You cannot call this
. this
is a pointer to the current object, it is not callable.
您需要传递成员函数函数添加到你的 std :: thread
构造函数中。
You need pass a member function or other function into your std::thread
constructor.
你也可以创建一个函子,可调用。
You could also create a functor and pass that, as that is callable.
编辑:只是注意到确实重载了 operator()
,调用它,你做了:
Just noticed have indeed overloaded operator()
, to call it, you have do the follwowing:
test t;
std::thread my_thread(t); //invokes operator() on test.
//also remove this statement from your ctor: pt = new std::thread(this);
这篇关于std :: thread,start线程在`this'(从类本身)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!