![](/img/trans.png)
[英]How to trigger code when a thread exits, without using functions *_at_thread_exit?
[英]How to use boost::thread::at_thread_exit or call a function when thread is done
这是最少的代码来说明我所需要的。 它不起作用,因为(正确的消息是编译时出现的错误信息)at_thread_exit不是boost :: thread的成员。 我知道与名称空间this_thread有关,我已经浏览了boost页面上的文档,但是无法了解如何使用at_thread_exit。 我还没找到任何简单的示例说明如何使用Google。
#include <boost/thread.hpp>
#include <iostream>
class A{
public:
void callme(){
int a = 1;
}
void runThread() {
boost::thread td(&A::callme,this);
td.at_thread_exit(&A::done,this);
td.join();
}
void done() {
std::cout << "I am done!!!\n";
}
};
int main(int argc, char **argv) {
A *a = new A();
a->runThread();
delete a;
return EXIT_SUCCESS;
}
boost::thread td([this]{
callme();
done();
});
at_thread_exit
仅在同一线程内工作; 否则,它将需要同步,这将使每个线程在只有一些线程使用它时为此付出代价。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.