Lets talk about fibers

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 8 05:20:29 PST 2017


On Sun, 08 Jan 2017 09:18:19 +0000, Suliman wrote:

>> Simply picking a worker thread + worker fiber when task is assigned and
>> sticking to it until finished should work good enough. It is also
>> important to note though that "fiber" is not the same as "task". Former
>> is execution context primitive, latter is scheduling abstraction. In
>> fact, heavy load systems are likely to have many more tasks than fibers
>> at certain spike points.
> 
> Could you explain difference between fibers and tasks. I read a lot, but
> still can't understand the difference.

A task is a unit of work to be scheduled.

A fiber is a concurrency mechanism supporting multiple independent 
stacks, like threads, that you can switch between. Unlike threads, a 
fiber continues to execute until it voluntarily yields execution.

You might have a task: send a registration message to a user who just 
registered. That gets scheduled onto a fiber. Your email sending stuff is 
vibe.d all the way down, and also you have to make some database queries. 
The IO involved causes the fiber that the task was scheduled on to yield 
execution several times. Finally, the task finishes, and the fiber can be 
destroyed -- or reused for another task.


More information about the Digitalmars-d mailing list