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++?
我刚刚开始研究go以及go的并发性。 我只是想知道您是否可以实现 在C ++中是等效的，并找到了boost.fiber。 goroutines和boost光纤之间有什么区别？ 您可以在C ++中用这些光纤实现类似于goroutine的东西吗？ p> div>
- 赞 踩
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::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.已采纳该答案赞 踩 打赏 评论分享