我怎样才能在c ++中运行线程?

时间:2015-11-29 14:01:02

标签: c++ multithreading c++11

我上课了:

class MyClass
{
public:
    MyClass();
    list<Obj> mainList;
    void MyMethod(){//This method changes elements in mainList};
}

void StartThread(MyClass& tmp)
{
    tmp.MyMethod();
}

main()
{
    MyClass test();
    std::thread thr(StartThread, std::ref(test));
    the.join(); //or detach(), both don't work
}

我可以用C ++ 11这样做吗?

我每次都会收到的错误(LongPollSession = MyClass):

/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(LongPollSession); _Args = {std::reference_wrapper<LongPollSession*>}]’
main.cpp:19:47:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’
         _M_invoke(_Index_tuple<_Indices...>)

0 个答案:

没有答案