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++项目,很不错,值得参考。
参考
有疑问加站长微信联系(非本文作者)