Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fibers use cases

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):

  • Distinguishing coroutines and fibers
  • Producer/Consumer using Boost.Fibers
  • Boost.Fiber documentation
  • Many, many others

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.

like image 662
senseiwa Avatar asked Apr 30 '18 07:04

senseiwa


2 Answers

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.

like image 122
Solomon Slow Avatar answered Oct 01 '22 16:10

Solomon Slow


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.

like image 44
Brandon Kohn Avatar answered Oct 01 '22 16:10

Brandon Kohn