mercredi 29 novembre 2017

Thread in C++11 not in class member

I'm recently using C++11 threads and I have a question about something (strange for me) happened. I created a method inside a class, able to start thread. The starting thread method and the callback function of the thread are class methods. To clarify, in my Person.cpp file I have:

void Person::callbackForThread(){
    ...
}


void Person::startThread(){
    this call threads. Something like:
    thread(callbackForThread);
}

The problem is that C++11 doesn't allow me to declare these two function as class methods. This means that if I declare them as normal function, i.e:

 void callbackForThread(){
    ...
}


void startThread(){
    this call threads. Something like:
    thread(callbackForThread);
}

Everythings works. I would know how could I declare thread and callback inside a class in C++, if it is possible. For the farther, I have omitted the inclusion of the libraries and the name of the real class. The class shown in this question is fictitious.

Aucun commentaire:

Enregistrer un commentaire