Few questions about concurrency

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 27 00:40:19 PDT 2016


On 27/08/2016 7:30 PM, Suliman wrote:
> Hello! I am still attempt to better understand how concurrency works. I
> read a lot of info but still misunderstood some key-points.
> Here is my thought about it and questions:
>
> 1. There is OS threads. Context-switching is very expensive because
> during it we should to save ALL CPU registers to RAM. Accessing to RAM
> is slowly up 200-300 times then accessing to registers, so
> dumping/restoring registers take a lot of time. Right?

Saving/restoring registers is fast.

> 1.1 During context-switching OS is go to kernel-mode (ring-0). I have
> heard opinion that it's cost a lot, but can't understand is it true and
> why it cost a lot?

Ring transition e.g. system calls are not all that expensive.
Its what the kernel does with that system call that is problematic to 
performance.

> 2. Message passing is superstructure of threads. Right? When it can be
> helpful? Any user-case please.
> 2.1 Can simple threads pass data/messages to each other without message
> passing?

Message passing is an alternative to relying on globals + mutex's. In 
the end its implemented by a global + stack FIFO (mailbox).

> 3. Fibers is threads that works in user-mode (rather in single
> kernel-thread OS do not see them, it see only 1 own Thread).

Nope. A fiber is an execution context, aka registers save/restore just 
like with a thread but its implemented in userland instead of by the kernel.

> 3.1 Fibers are switch explicitly. If do not call yield mother-thread
> will be hang on.

A fiber runs on a thread yes, but it is simply a way to switch the 
function executing on a thread.

> 3.2 Where fibers store CPU register state in moment of switching between
> fibers? In memory? But then what difference with context-switching (that
> take a lot of time to save/restore registers state)?
>
> Please correct me where I am wrong and highlight where I am right.



More information about the Digitalmars-d-learn mailing list