mercredi 29 mars 2017

Access variable in main thread from second thread

I have a function (messageArrived) that call's a function (setAnimation) inside a new thread. How can i access a boolean that is defined inside the messageArrived function and access it in the second thread?

If there is a new message i want to terminate the second thread (setAnimation). I fugured that whit a boolean is the only way to "terminate" a thread.

#include <thread>

setAnimation(std::string msg){
    start = true;
    while(start){
       //do something
    }
    return;
}    

int messageArrived(message){
     bool start = false;
     std:thread t1(setAnimation, message);
     t1.detach();
     return 1;
}

Above code is just an example to clarify my question.

Aucun commentaire:

Enregistrer un commentaire