Live data from Hacker News

Fiber in C++: Understanding the Basics

agraphicsguynotes.com

11–20 of 77 posts

Re: Fiber in C++: Understanding the Basics

#11

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

Boost asio is available as a standalone lib as well. What do you mean it doesn't support "it" out of the box?

Re: Fiber in C++: Understanding the Basics

#12
> 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?

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?

It's exactly what it says, it's a coroutine with its own stack.

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?

I assume it's this:

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

#16

I'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…

The Windows fiber library is just that, a library. It's not an OS component. It's provided because stack switching in Windows is a somewhat fraught [1] and not officially documented procedure which requires modifying the TIB [2].

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

#17

Again, 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.

Pony/Ponylang claims that they can do actors fast and safe (even safer than rust). There is a type system that allows one to share data between actors safely, or so they claim.

Re: Fiber in C++: Understanding the Basics

#19
Somewhat related to Fibres are the Trails of synchronous reactive programming languages. Both allow efficient logical concurrency based on cooperative scheduling. The nice thing with Trails is that the scheduling can be determined by the compiler by extracting dependencies from the synchronous reactive program thus increasing determinism of your app.

Re: Fiber in C++: Understanding the Basics

#20

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

Boost asio is available as a standalone lib as well. What do you mean it doesn't support "it" out of the box?

Asio has no coroutine implementation and C++20 Coros are stackless. So, to build fibers with asio, you need boost::coroutine for stackful coros.
Post reply on HN