我有一个关于将 boost::threads 与一些 c++11 标准项目混合和匹配的问题,这行得通吗?我实际上还没有测试任何东西,但我正在使用一个系统,该系统使用所有 boost::threads 和线程组以及中断功能,这些功能在标准中没有开箱即用,因此没有变化。我们的 boost 1.50 版本没有最新的 std::atomic 和内存排序的东西。我想知道我是否可以将 std::atomic 和 std::内存排序操作 load/fectch_add 等(获取/释放,放松)与 boost 线程一起使用,并获得与 std::thread 相同的结果。这些都是我 linux 机器上的 pthreads,所以我认为答案是肯定的,我可以混合搭配。虽然,我只是想确认并看看是否有人在混合 boost::thread 和 std::thread api 之间遇到任何兼容性问题。
最佳答案
自从 C++11 广泛可用以来,我一直在思考这个有趣的问题。
一般来说,我注意到 std 组件的 boost 版本通常具有比 std 版本提供更多功能的扩展。例如,boost::bind
提供比 std::bind
更多的功能, boost <type_traits>
比标准品更丰富,boost::thread
允许线程取消/中断,而标准线程不允许,等等。
正如您提到的,特别是关于 boost 线程与标准线程
... I am working with a system that uses all boost::threads and thread groups and interrupt features that you don't get out of the box with the standard...
我想指出取消 boost 线程中断并非没有代价,boost::condition_variable
真的是boost::condition_variable_any
在 boost 中启用线程取消时。 boost::condition_variable_any
维护自己的互斥量并比原始 POSIX 锁定更多 pthread_cond_t
那boost::condition_variable
被设计成一个轻量级的包装器。线程中断功能 adds measurable 5-10% speed overhead to boost::condition_variable
, condition variable: std vs boost chart .
Our version of boost 1.50 and doesn't have the latest std::atomic and memory ordering stuff. I was wondering if I could use the std::atomic and std:: memory ordering operations load/fectch_add etc(acquire/release,relaxed) with the boost threads and have the same results as if they were std::thread
std::atomic
库不使用或依赖特定的线程库仅用于某些内置原子类型(整数和指针不宽于自然平台宽度,例如 32 或 64 位),或平台,所以你可以随意混合和匹配线程与原子库,只要你小心使用 std::atomic<T>
其中 T
的原子性由硬件支持(同样,整数和指针),您可以使用 std::atomic<T>::is_lock_free()
进行检查.
关于c++ - boost::thread 和 std::thread 兼容性问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24070400/