它是智能更换的boost ::螺纹和boost ::互斥与C ++ 11等值? [英] Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?
问题描述
动机:为什么我考虑的是我的天才的项目经理认为,升压是另一种依赖,这是可怕的,因为你依赖于它(我试图解释提升的质量,再经过一些放弃时间:()。较小的原因,我想做到这一点的是,我想学习C ++ 11的特性,因为人们将开始在其写入code。
所以:
Motivation: reason why I'm considering it is that my genius project manager thinks that boost is another dependency and that it is horrible because "you depend on it"(I tried explaining the quality of boost, then gave up after some time :( ). Smaller reason why I would like to do it is that I would like to learn c++11 features, because people will start writing code in it. So:
- 有一个1:#
之间的一对一映射包括<螺纹> #包括LT&;&互斥GT;
和
升压等值? - 你会考虑一个好主意,用C来代替升压东西++ 11结果
东东。我的用法是原始的,但是否有例子,当性病犯规
提供做什么提升?或(亵渎),反之亦然?
P.S。
我用GCC所以头在那里。
P.S. I use GCC so headers are there.
推荐答案
有Boost.Thread和C ++ 11标准的线程库之间有几个不同点:
There are several differences between Boost.Thread and the C++11 standard thread library:
- 升压支持线程取消,C ++ 11线程不会
- C ++ 11支持
的std ::异步
,但加速并不 - 升压有一个
的boost :: shared_mutex
对于多读/单写锁。类似的的std :: shared_timed_mutex
仅因为C ++ 14(的N3891 ),而的std :: shared_mutex
仅因为C ++ 17(的N4508 )。 - C ++ 11超时是不同的,以提高超时(尽管这应该很快就会改变现在Boost.Chrono已被接受)。
- 一些名字是不同的(如
的boost :: unique_future
VS的std ::未来
) -
的std ::线程
的参数传递的语义是的boost ::线程
---升压不同使用的boost ::绑定
,这需要拷贝的参数。的std ::线程
允许移动只如的std ::的unique_ptr
类型作为参数传递。由于使用了的boost ::绑定
中,占位符的语义,如_1
嵌套绑定前pressions可以是不同的了。 - 如果您不显式调用
加入()
或分离()
那么的boost ::线程
析构函数赋值运算符将调用分离()
被销毁/分配。用C ++ 11的std ::线程
的对象,这将导致调用的std ::终止()
并中止应用程序。
- Boost supports thread cancellation, C++11 threads do not
- C++11 supports
std::async
, but Boost does not - Boost has a
boost::shared_mutex
for multiple-reader/single-writer locking. The analogousstd::shared_timed_mutex
is available only since C++14 (N3891), whilestd::shared_mutex
is available only since C++17 (N4508). - C++11 timeouts are different to Boost timeouts (though this should soon change now Boost.Chrono has been accepted).
- Some of the names are different (e.g.
boost::unique_future
vsstd::future
) - The argument-passing semantics of
std::thread
are different toboost::thread
--- Boost usesboost::bind
, which requires copyable arguments.std::thread
allows move-only types such asstd::unique_ptr
to be passed as arguments. Due to the use ofboost::bind
, the semantics of placeholders such as_1
in nested bind expressions can be different too. - If you don't explicitly call
join()
ordetach()
then theboost::thread
destructor and assignment operator will calldetach()
on the thread object being destroyed/assigned to. With a C++11std::thread
object, this will result in a call tostd::terminate()
and abort the application.
要澄清关于唯才是举参数来看,以下是有效的C ++ 11,并从临时 INT
的所有权>的std ::的unique_ptr 到 F1
当新的线程启动的参数。但是,如果你使用的boost ::线程
那么它不会工作,因为它使用的boost ::绑定
内部和的std ::的unique_ptr
无法复制。还提供了与海湾合作委员会是prevents这个工作,因为它在实现使用的std ::绑定
有太多的C ++ 11线程库的错误。
To clarify the point about move-only parameters, the following is valid C++11, and transfers the ownership of the int
from the temporary std::unique_ptr
to the parameter of f1
when the new thread is started. However, if you use boost::thread
then it won't work, as it uses boost::bind
internally, and std::unique_ptr
cannot be copied. There is also a bug in the C++11 thread library provided with GCC that prevents this working, as it uses std::bind
in the implementation there too.
void f1(std::unique_ptr<int>);
std::thread t1(f1,std::unique_ptr<int>(new int(42)));
如果您使用的是加速,那么你也许可以切换到C ++ 11线程比较怕疼,如果你的编译器支持的话(例如最近在Linux上的GCC版本有C ++ 11线程库的主要完成实施提供 -std =的C ++ 0x
模式)。
If you are using Boost then you can probably switch to C++11 threads relatively painlessly if your compiler supports it (e.g. recent versions of GCC on linux have a mostly-complete implementation of the C++11 thread library available in -std=c++0x
mode).
如果你的编译器不支持C ++ 11线程,那么你也许可以得到一个第三方实现,如刚::螺纹,但是这仍然是一个依赖关系。
If your compiler doesn't support C++11 threads then you may be able to get a third-party implementation such as Just::Thread, but this is still a dependency.
这篇关于它是智能更换的boost ::螺纹和boost ::互斥与C ++ 11等值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!