I am reading a lot about Fibers, or green threads, or whatever other name we can give to userland threads. I started reading documentations and tutorials (these are C++ links, but I don't need a specific language):
However, it seems I cannot grasp the essentials about fibers. I know that fibers are a way to cooperatively multitask, but documentation about interplay between threads and fibers in actual cases are, as far as I found, scarce.
What are some practical use cases of fibers?
For instance, every doc actually uses async I/O as an example, but what if I don't have I/O-bound problems? For instance, what if my problem is counting words in a huge file? Here I would just split the file among threads, can fibers help somehow? I suppose that CPU-bound computations such as numerical problems (e.g., matrix/vector operations) are not suitable for fibers, but again, I might be completely wrong.
what if my problem is counting words in a huge file? ..., can fibers help somehow?
No.
every doc actually uses async I/O as an example
Async I/O is the problem that threads originally were meant to solve back when multi-CPU systems had not yet escaped from the laboratory. Threads were an alternate way to structure a program that had to wait for input from several different, non-synchronized sources and, had to respond to those inputs in a timely fashion.
Depending on how they were implemented, threads back in those days could be anywhere on a scale from "mostly the same as" to "completely identical with" what we call "green threads" or "fibers" today.
When multi-CPU systems hit the market, threading was seen as a natural and obvious way to exploit the parallel processing capabilities.
Fibers are meant to have lower overhead on creation and context switching than OS threads. So in theory, if you have a solution where you have lots of blocking on locks, you may see a performance improvement from fibers because the OS threads on which the fibers run will use more of their allotted runtime. This is because when a fiber blocks on a fiber mutex/lock, the underlying OS thread will invoke a fiber scheduler which will run a different fiber, all without doing an OS-thread context switch. This is the basic idea behind M:N threading models.
Another case would be if you need to create and destroy threads with great frequency or in large numbers. Because fibers are faster to create and typically more lightweight than OS threads, you can use them in much larger numbers, and for much finer grained parallelism (in theory.)
One practical application is for large agent based simulation using the actor model. With fibers, each agent/actor can be run on its own fiber.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With