久久久久久久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 排序功能對列表進行排序)
主站蜘蛛池模板: 免费av电影网站 | 欧美不卡| 久久99精品久久久久久噜噜 | 日本韩国欧美在线观看 | 国产精品久久久久久一区二区三区 | 国产精品久久久久一区二区三区 | 成人一区二区三区在线观看 | 亚洲视频中文字幕 | 欧美一区二区久久 | 99色播 | 欧美激情区| 亚洲成人自拍 | 免费成年网站 | 蜜桃臀av一区二区三区 | 99精品视频在线观看免费播放 | 欧美日韩高清一区 | 国产午夜精品一区二区三区嫩草 | 亚洲电影一级片 | 国产精品久久网 | 久久这里只有精品首页 | 国产电影一区二区在线观看 | 日本激情视频在线播放 | 成人九色 | 国产专区免费 | 国产一区二区三区在线 | 国产精品乱码一区二三区小蝌蚪 | 国产三级| 日本成人片在线观看 | 亚洲精品视 | 免费观看一级毛片 | 亚洲精品久久久蜜桃 | 国产区在线观看 | 91精品久久久久久久久中文字幕 | 久久久久久久网 | 欧美综合久久 | 久久亚洲精品国产精品紫薇 | 一区二区精品在线 | 99视频在线免费观看 | 国产高潮av | 精品久久久久一区 | 91成人影院 |