問題描述
動機:我之所以考慮它是因為我的天才項目經理認為 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 - 你認為用 c++11 替換 boost 的東西是個好主意嗎
東西.我的用法很原始,但有沒有 std 不使用的例子提供什么提升呢?或者(褻瀆)反之亦然?
附言我使用 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:
- Boost 支持線程取消,C++11 線程不支持
- C++11 支持
std::async
,但 Boost 不支持 - Boost 有一個
boost::shared_mutex
用于多讀/單寫鎖定.類似的std::shared_timed_mutex
僅從 C++14 開始可用 (N3891),而std::shared_mutex
僅在 C++17 后可用(N4508). - C++11 超時與 Boost 超時不同(盡管現在 Boost.Chrono 已被接受,這應該很快就會改變).
- 有些名稱不同(例如
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++11std::thread
對象,這將導致調用std::terminate()
并中止應用程序.
- 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::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 是否明智?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!