是否可以使用boost :: threads中的std :: this_thread *函数? [英] Is it okay to use std::this_thread* functions from boost::threads?
问题描述
是否可以混合并匹配boost::thread
和std::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::thread
s, 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::thread
s to std::thread
s 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屋!