动机:我之所以考虑它是因为我的天才项目经理认为 boost 是另一种依赖,而且它很可怕,因为你依赖它"(我尝试解释 boost 的质量,然后在一段时间后放弃了时间 :( ). 我想做它的较小原因是我想学习 c++11 特性,因为人们会开始在其中编写代码.所以:
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:
#include 之间是否存在 1:1 映射?#include
和提升等价物?附言我使用 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:
std::async
,但 Boost 不支持boost::shared_mutex
用于多读/单写锁定.类似的 std::shared_timed_mutex
仅从 C++14 开始可用 (N3891),而 std::shared_mutex
仅在 C++17 后可用(N4508).boost::unique_future
与 std::future
)std::thread
的参数传递语义不同于 boost::thread
--- Boost 使用 boost::bind
,这需要可复制的参数.std::thread
允许将诸如 std::unique_ptr
之类的仅移动类型作为参数传递.由于使用了boost::bind
,嵌套绑定表达式中的占位符(例如_1
)的语义也可能不同.join()
或 detach()
那么 boost::thread
析构函数和赋值运算符将调用detach()
在被销毁/分配给的线程对象上.对于 C++11 std::thread
对象,这将导致调用 std::terminate()
并中止应用程序.std::async
, but Boost does notboost::shared_mutex
for multiple-reader/single-writer locking. The analogous std::shared_timed_mutex
is available only since C++14 (N3891), while std::shared_mutex
is available only since C++17 (N4508).boost::unique_future
vs std::future
)std::thread
are different to boost::thread
--- Boost uses boost::bind
, which requires copyable arguments. std::thread
allows move-only types such as std::unique_ptr
to be passed as arguments. Due to the use of boost::bind
, the semantics of placeholders such as _1
in nested bind expressions can be different too.join()
or detach()
then the boost::thread
destructor and assignment operator will call detach()
on the thread object being destroyed/assigned to. With a C++11 std::thread
object, this will result in a call to std::terminate()
and abort the application.为了澄清关于仅移动参数的观点,以下是有效的 C++11,并将 int
的所有权从临时 std::unique_ptr
转移新线程启动时f1
的参数.但是,如果您使用 boost::thread
那么它将无法工作,因为它在内部使用 boost::bind
和 std::unique_ptr
> 无法复制.GCC 提供的 C++11 线程库中也有一个错误,它阻止了这项工作,因为它在那里的实现中也使用了 std::bind
.
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)));
如果您正在使用 Boost,那么如果您的编译器支持它,那么您可能可以相对轻松地切换到 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 线程,那么您可以使用第三方实现,例如 Just::线程,但这仍然是一个依赖项.
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.
这篇关于用 c++11 等价物替换 boost::thread 和 boost::mutex 是否明智?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!