Lets talk about fibers

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 8 08:40:44 PST 2017


On Sunday, 8 January 2017 at 09:18:19 UTC, 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.

Fiber is context switching primitive very similar to thread. It 
is different from thread in a sense that it is completely 
invisible to operating system and only does context switching 
when explicitly told so in code. But it still can execute 
arbitrary code. When we talk about fibers in D, we usually mean 
https://dlang.org/library/core/thread/fiber.html

Task is abstraction over some specific piece of work to do. Most 
simple task one can think of is simply a function to execute. 
Other details may vary a lot -different languages and libraries 
implement tasks differently, and D standard library doesn't 
define it all. Most widespread task definition in D comes from 
vibe.d - http://vibed.org/api/vibe.core.task/Task

To summarize - fiber defines HOW to execute code but doesn't care 
which code to execute. Task defines WHAT code to execute but 
normally has no assumptions over how exactly it gets run.


More information about the Digitalmars-d mailing list