duanfei9278 2016-03-14 16:24
浏览 260
已采纳

goroutine和boost.fiber之间的区别

I just started having a look at go and how the concurency in go works. I just wondered if you could implement something equal in C++ and found boost.fiber. What's the difference between goroutines and boost fibers? Could you implement something goroutine like with those fibers in C++?

  • 写回答

1条回答 默认 最新

  • doujing1858 2016-03-14 19:32
    关注

    So from what I could gather from the source code of the boost.fiber library, it seems that it is indeed MUCH more versatile and powerful than goroutines. The mantra of goroutines is to not share data between the coroutines, but rather pass data into them as necessary. This is apparently possible within fibers as well with channels (boost::fibers::unbounded_channel<T> and boost::fibers::bounded_channel<T>), although one thing I'd keep in mind is data is safely shared between all fibers in a single thread. This is managed (by the use of mutexes, I guess) to ensure only one fiber runs at a time, so you may not need channels for a particular use case.

    Fiber also seems to give you control over the synchronization of fibers within threads it seems. Apparently I/O that blocks will block an entire thread that the fiber running the operation is in, which makes sense. The cooler thing is that you could use Fiber to simulate blocking (e.g. for strict order of routine scheduling) if needed. There is a pretty good example of custom scheduling of fibers based on priority queuing here https://github.com/olk/boost-fiber/blob/master/examples/priority.cpp as well. However, I'd be willing to bet that scheduling defaults to interleaving them

    So just to summarize, it seems the main points to take away are that yes, Fibers already has goroutine-like stuff already implemented. You have channels, you can multithread (and I'm sure this can be extended to multicore fun as well), and you can run async operations in the same thread (again, I'm assuming they default to interleaved scheduling in the same thread). Fibers seems to have much more, context-switching, fiber-safe shared memory, while go tends to keep things restricted to data passing using channels.

    Honestly you'd have to surf the codebase a bit like I did to see what else Fibers has, but yeah I'd say these are some main diffs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错