我正在寻找一种在 boost 线程(boost 版本 1.60,普通线程,无线程组或池)完成时调用回调函数的方法。我读过这个
How can I tell reliably if a boost thread has exited its run method?
但我需要某种回调。知道怎么做吗?我是否必须创建某种条件变量?
感谢您的帮助!
最佳答案
最简单的解决方案是包装您的原始线程函数:
#include <boost/thread.hpp>
#include <iostream>
void callback()
{
std::cout << "callback invoked" << std::endl;
}
void orig_thread_func()
{
std::cout << "thread function invoked" << std::endl;
}
void wrapper(void (*func)())
{
func(); // invoke your original thread function
callback(); // invoke callback
}
int main()
{
boost::thread t(&wrapper, &orig_thread_func);
t.join();
return 0;
}
关于c++ - Boost线程完成回调可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604748/