dimanche 29 novembre 2015

How can i run thread in c++? [duplicate]

This question already has an answer here:

I have my class:

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
}

So can I do it this way using C++11?

The error that I get every time(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...>)

Aucun commentaire:

Enregistrer un commentaire