英文:
Difference between goroutines and boost.fiber
问题
我刚开始学习Go语言和Go语言中的并发编程。我想知道是否可以在C++中实现类似的功能,于是我找到了boost.fiber。goroutines和boost fibers之间有什么区别?在C++中是否可以使用这些fibers实现类似goroutine的功能?
英文:
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
得分: 15
从我从boost.fiber库的源代码中收集到的信息来看,它似乎比goroutines更加灵活和强大。goroutines的原则是不在协程之间共享数据,而是根据需要将数据传递给它们。在fibers中,通过使用通道(boost::fibers::unbounded_channel<T>
和boost::fibers::bounded_channel<T>
)也可以实现这一点。但需要注意的是,在单个线程中,所有纤程之间的数据是安全共享的。这是通过管理(我猜是通过互斥锁)来确保一次只有一个纤程运行,所以在某些情况下可能不需要通道。
Fiber似乎还可以控制线程内纤程的同步。显然,阻塞的I/O会阻塞执行操作的纤程所在的整个线程,这是有道理的。更酷的是,如果需要,你可以使用Fiber来模拟阻塞(例如,为了严格的例程调度顺序)。这里有一个非常好的基于优先级队列的自定义纤程调度示例:https://github.com/olk/boost-fiber/blob/master/examples/priority.cpp。然而,我敢打赌,默认情况下它们会交错调度。
所以,总结一下,看起来主要的要点是,是的,Fibers已经实现了类似goroutine的功能。你有通道,可以进行多线程操作(我相信这也可以扩展到多核操作),并且可以在同一个线程中运行异步操作(再次,我假设它们默认是交错调度在同一个线程中)。Fibers似乎还具有更多功能,如上下文切换、纤程安全的共享内存,而Go则倾向于将事物限制在使用通道进行数据传递。
老实说,你需要像我一样浏览一下代码库,才能看到Fibers还有什么其他功能,但是是的,我认为这些是一些主要的区别。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论