Still looking for a lib that supports fibers... boost::asio is... well, boost, and therefore annoying. It also does not support it out of the box
Fiber in C++: Understanding the Basics
11–20 of 77 posts
Re: Fiber in C++: Understanding the Basics
#12Can someone explain what the "stackful coroutine" term means (ideally with an example)? And how can one implement it?
Re: Fiber in C++: Understanding the Basics
#13Still looking for a lib that supports fibers... boost::asio is... well, boost, and therefore annoying. It also does not support it out of the box
Re: Fiber in C++: Understanding the Basics
#14> Fiber is quite a lightweight thread of execution. Like coroutine, fiber allows yielding at any point inside it. To some degree, we can regard fiber as a form of stackful coroutine [...] Can someone explain what the "stackful coroutine" term means (ideally with an example)? And how can one implement it?
A fiber isn't to some degree a stackful coroutine, it is a stackful coroutine.
A stackless coroutine does not have a its own stack, it uses the calling context's stack and therefore can only yield to the calling context from the top-level coroutine function. A stackful coroutine has its own stack, and so can yield to the calling context from anywhere.
As far as how to implement, I always liked Malte Skarupke's blog post on the subject: https://probablydance.com/2013/02/20/handmade-coroutines-for...
Re: Fiber in C++: Understanding the Basics
#15> Fiber is quite a lightweight thread of execution. Like coroutine, fiber allows yielding at any point inside it. To some degree, we can regard fiber as a form of stackful coroutine [...] Can someone explain what the "stackful coroutine" term means (ideally with an example)? And how can one implement it?
In Python, coroutines cannot yield from within another function call: if coroutine A calls function B, B cannot yield. In Lua, it's possible to yield a coroutine at any function depth: B can yield.
To implement something like this in a compiled language, you just need multiple stacks instead of the usual 1. Most architectures, such as x86, have a stack pointer register; when yielding a coroutine, just change the sp register to point to some other stack -- when resuming, restore the sp.
This is not a new concept. Pokémon on the gameboy did this for its UI fiber, for example.
Re: Fiber in C++: Understanding the Basics
#16I'm confused in how this article defines "fiber." At the beginning of the article, fibers are described as a lightweight userspace thread, similar to what is provided in the runtimes of Haskell and Go, and has been recently revived in Java as "virtual threads." However, later in the article (after a long and apparently irrelevant digression about stack management), they describe Windows API functions that are require…
Doing it correctly isn't hard, and has been done many times in bullet-proof libs (notably boost::context), but MS would maybe rather you not.
[1]: https://devblogs.microsoft.com/oldnewthing/20080215-00/?p=23...
[2]: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block
Re: Fiber in C++: Understanding the Basics
#17Again, academia and the tech press overlook to talk about Actors, and we live in a world with poorly implemented pretend Actor subsets, without any of the true Actor features (work stealing, message passing etc, and best of all, no contention since no shared resources, other than Actor references). Fibres, goroutines, promises, futures, job queues, etc, are poor substitutes for Actors.
No shared resources can be a performance problem, I can see why there are rusts and vales efforts to come to a slightly different tradeoff.
Re: Fiber in C++: Understanding the Basics
#18Re: Fiber in C++: Understanding the Basics
#19Re: Fiber in C++: Understanding the Basics
#20Still looking for a lib that supports fibers... boost::asio is... well, boost, and therefore annoying. It also does not support it out of the box
Boost asio is available as a standalone lib as well. What do you mean it doesn't support "it" out of the box?