久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用 c++11 等價物替換 boost::thread 和 boost::mutex 是否

Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?(用 c++11 等價物替換 boost::thread 和 boost::mutex 是否明智?)
本文介紹了用 c++11 等價物替換 boost::thread 和 boost::mutex 是否明智?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

動機:我之所以考慮它是因為我的天才項目經理認為 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:

  1. #include 之間是否存在 1:1 映射?#include 和提升等價物?
  2. 你認為用 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_futurestd::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() 并中止應用程序.
  • 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 analogous std::shared_timed_mutex is available only since C++14 (N3891), while std::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 vs std::future)
  • The argument-passing semantics of 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.
  • If you don't explicitly call 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::bindstd::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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 大香在线伊779 | 黄色片av| 欧美精品99| 鲁大师一区影视 | 欧美精品一区二区三区在线四季 | 色毛片 | 久久首页 | 五月综合色啪 | 国产欧美久久一区二区三区 | 日韩a在线 | 亚洲国产精品视频一区 | 99久久精品一区二区成人 | 97伦理电影 | 欧美精品一二三 | 99在线国产| 亚洲精品日韩综合观看成人91 | 久久久黄色 | 天堂久久久久久久 | 人人干天天干 | 亚洲人成人一区二区在线观看 | 一区视频| 欧美一区二区在线免费观看 | 欧美午夜精品 | 91啪亚洲精品 | 91精品国产91 | 亚洲乱码一区二区三区在线观看 | 国产999精品久久久影片官网 | 欧美日韩国产高清 | 成人片免费看 | 国产精品久久久久一区二区三区 | 高清免费av | 国产在线a| 国产精品69毛片高清亚洲 | 中国黄色毛片视频 | 日韩免费一区二区 | 国产一区二区久久 | 午夜成人免费视频 | 国产精品无码久久久久 | 国产九一精品 | 国产精品色 | 亚洲免费视频一区 |