是否可以使用boost :: threads中的std :: this_thread *函数? [英] Is it okay to use std::this_thread* functions from boost::threads?

查看:131
本文介绍了是否可以使用boost :: threads中的std :: this_thread *函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以混合并匹配boost::threadstd::thread中的内容,还是应该对每个功能使用一组功能?

我问是因为我的代码使用了boost::thread,但是我发现boost::this_thread::sleep_for在设置系统时间后不能正常工作,但是std::this_thread::sleep_for可以,所以我想改变我的睡眠时间函数调用,并尽可能避免将所有boost::thread更改为std::thread.

解决方案

在实践中,如果实现使用相同的实现(例如,在Linux上为pthread),您可能会不满意.

但是,您打破不变式.一个简单的例子:Boost Thread的中断点不适用于非增强型同步原语(包括std::this_thread::sleep_*).

因此,我建议不要使用 mixing 库来控制相关线程,以免您想冒意外的风险

当然,如果库有完全不同的关注点(例如,它们在内部使用线程,在黑匣子"中),那么将这些库合并到一个进程中就不会有问题.


¹我看到死锁正在发生,数据争用/泄漏不需要大量的想象力(想想线程本地数据支持/call_once/set_value_at_thread_exit ...)

Is it okay to mix and match things from boost::thread and std::thread, or should one set of functions be used for each?

I ask because my code uses boost::threads, but I've found that boost::this_thread::sleep_for doesn't behave properly when setting the system time back, but std::this_thread::sleep_for does, so I'd like to change my sleep function call and avoid changing all my boost::threads to std::threads if possible.

解决方案

In practice you might get away with things iff/because the implementations use the same implementations (e.g. pthread on linux).

However, you will be breaking invariants. Simple example: Boost Thread's interruption points won't function with non-boost synchronisation primitives (including std::this_thread::sleep_*).

Therefore I'd avice against actually mixing libraries for controlling related threads, lest you want to risk running into suprises ¹

Of course, if libraries have completely separate concerns (e.g. they use threads internally, "in the black box"), there should be no issue combining those libraries in one process.


¹ I can see deadlocks happening, and data races/leaks do not require a huge stretch of imagination (think thread local data support/call_once/set_value_at_thread_exit...)

这篇关于是否可以使用boost :: threads中的std :: this_thread *函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆