c++ - Boost线程完成回调可用

标签 c++ multithreading boost callback

我正在寻找一种在 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/

相关文章:

c++ - hash_map.erase 是否会使所有迭代器无效?

c# - .net 中的锁定行为如何?

c++ - pthread_join 错误,从线程创建线程

c++ - 将 boost::multiprecision 数据类型写入二进制文件

c++ - 在 Windows 上使用 Boost.Asio 的半并发 ICMP ping

c++ - SDL side-scroller 滚动不一致

c++ - 阅读(?)转义字符

c++ - Color(int, int, int) vs Color(float, float, float) 模糊调用

c++ - 我如何在 std::atomic<T> 上实现一个简单的自旋锁,以便编译器不会优化它?

c++ - Boost二进制序列化 - 固定长度错误的双数组