C++ Coroutine

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Coroutine libraries

boost.coroutine

boost.asio對boost.coroutine庫有個封裝,

boost.coroutine是stackfull的。stackless的coroutine不能在reenter中使用局部變數,即使加個大括弧可以使用局部變數,再次進入函數的時候變數的值也會丟失,而stackfull的則不會。這就帶來了很大的方便。
yyzybb537/libgo(Go-style concurrency in C++11)也實現了一個coroutine,作者有詳細的效能測試對比,而且說:

1.asio的coroutine會被網路IO阻塞,雖然使用者可以使用非同步網路io,但是第三方庫並不會,因此現有的使用同步網路IO的第三方庫統統不能用。
2.asio的coroutine只是簡單的網路層面的協程,缺失很多feature,比如channel mutex等,僅僅是個協程模型的網路程式庫. 這個corotuine卻是一個完整的協程模型的架構,特性完整.

libgo coroutine

libgo是魅族公司內部的一個項目。
知乎上如何評價c++的協程庫libgo?中作者的回答:
作為libgo的作者,好壞不便於評價,至少我們項目組用的都挺爽的~在此展示一些設計理念和測試結果吧.
Q: 更重要的是hook了阻塞的api,使得可以與第三方的同步網路程式庫無縫相容,不知道有沒有坑?A: libgo在hook的時候,會讓api的行為與系統原生api完全保持一致,每一個邊界條件的傳回值和錯誤碼都是一致的。在魅族推送系統線上的100多台伺服器中經過了半年左右的考驗,目前最新的2.6版本已經非常穩定了。
Q: 見過不少協程的輪子,但從沒見過支援多線程調度的,我覺得這個特性很有競爭力,就是不知道調度效能如何?A: 多線程調度使用的是worksteal演算法,效能上的損耗很小,遠遠快於golang。
以下是libgo協程切換速度和管道讀寫速度與golang對比測試的結果:------------- libgo ---------------BenchmarkSwitch_1 1000000 154 ns/opBenchmarkSwitch_1000 1000000 105 ns/opBenchmarkSwitch_10000 1000000 553 ns/opBenchmarkChannel_0 1000000 485 ns/opBenchmarkChannel_1 1000000 299 ns/opBenchmarkChannel_10000 10000 108 ns/opBenchmarkChannel_5000000 5000000 113 ns/op--------------------------------------------------- golang --------------BenchmarkSwitch_1 1000000 1438 ns/opBenchmarkSwitch_1000 1000000 1654 ns/opBenchmarkSwitch_10000 500000 2375 ns/opBenchmarkChannel_0 1000000 1536 ns/opBenchmarkChannel_1 500000 3037 ns/opBenchmarkChannel_N 50000000 65.0 ns/op--------------------------------------
測試代碼在libgo/test/golang目錄中,直接執行test.sh指令碼即可得到結果.
下面有熱心使用者給出的另一組結果截然不同的測試資料,在此補充一點,libgo使用boost1.59以上的版本才可以獲得這樣的高效能,cmake命令執行參數:$ cmake .. -DENABLE_BOOST_CONTEXT=ON

另外, 很重要的一點. 在實際使用中, 我們基於libgo封裝了一個網路程式庫(libgonet)作為我們的base架構.libgonet也是個開源項目, 希望在linux系統上基於libgo開發網路程式的, 可以關注一下哦~
編輯於2016-10-21

state-threads

這是某些人認為很好的協程庫。

The State Threads Library is a small application library which provides
a foundation for writing fast and highly scalable Internet applications
(such as web servers, proxy servers, mail transfer agents, and so on,
really any network-data-driven application) on UNIX-like platforms. It
combines the simplicity of the multithreaded programming paradigm, in
which one thread supports each simultaneous connection, with the
performance and scalability of an event-driven state machine
architecture. In other words, this library offers a threading API for
structuring an Internet application as a state machine. For more
details, please see the library documentation in the "docs" directory or
on-line at

http://state-threads.sourceforge.net/docs/

The State Threads Project is an open source project for maintaining and
enhancing the State Threads Library. For more information about this
project, please see

http://state-threads.sourceforge.net/

Coroutines in LLVM

LLVM的協程,還沒來得及看。

知識點小貼士

=default=delete

=default is a new C++11 feature.
It means that you want to use the compiler-generated version of that function, so you don't need to specify a body.
You can also use = delete
to specify that you don't want the compiler to generate that function automatically.
With the introduction of move constructors and move assignment operators, the rules for when automatic versions of constructors, destructors and assignment operators are generated has become quite complex. Using = default and = delete makes things easier as you don't need to remember the rules: you just say what you want to happen.
= delete is stronger: It means, using that function is forbidden, though it still takes part in overload resolution.

用不到的

Boost Fusion,heterogeneous containers,不知道效能如何,應該用不到。

雜項

libgo這個項目的make系統可以自動產生C++項目,很不錯,值得參考。

參考

[1] boost.coroutine學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.