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>
bool start = false;
void setAnimation(std::string msg){
start = true;
while(start){
//do something
}
return;
}
int messageArrived(std::string message){
start = false;
std::thread t1(setAnimation, message);
t1.detach();
return 1;
}
Above code is just an example to clarify my question.
创建线程时,可以使用std::ref
通过引用传递变量,但是,您仍然需要将变量放在函数之外,否则它将超出范围。
std::thread t1(setAnimation, message, std::ref(myVariable));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.