Skip to content

2.1 Fiber and thread pools

Alexander Damian edited this page Oct 16, 2018 · 3 revisions

quantum can be roughly seen as two thread pools being manged separately. Both thread pool sizes can be specified in the Dispatcher constructor which is the main class of this library. Furthermore the coroutine threads can optionally be pinned to CPU cores, but by default this option is turned off. Once the thread pools are initialized, they will remain at the same size. Each thread, has a dedicated queue from which it runs tasks. The coroutine tasks or fibers, are all run simultaneously and in parallel (cooperative), whereas the IO tasks are de-queued one-by-one and executed to completion (concurrent). This is a very standard approach to running traditional thread pools.

Below is a high-level diagram of the architecture:

arch diagram

As the above diagram shows, all IO tasks run in parallel and on dedicated threads (e.g. IO Thread 1). There is also a shared IO queue which is polled by all threads.

Coroutines on the other hand only run in parallel if-and-only-if they are on separate threads with respect to each other (e.g. Thread 1 vs Thread 2). The coroutines running on the same thread may give the impression they are executed in parallel but in reality the execution is sequential, with cooperative yields at specific points in the code (see Coroutines page for more details on cooperative yielding). Unless a coroutine yields, it will run to completion and no other coroutine can run on the same thread, which is why it's important to explicitly yield at regular intervals if the code is too long (e.g. large for loop).