This may be a very simple question. I'm curious how blocking calls are implemented. Specifically, how do they block? Is this just thread.sleep?
|
There are several ways to implement a blocking call. The obvious way to do it is to return when the work is done See Robert Harvey's Answer. In the case where there is no work to be done (e.g., waiting for a signal or for input), there are several choices:
|
|||
|
|
|
Methods don't In most cases, where you're seeing something like Where |
|||
|
|
|
In most system calls on most OS's, your blocking call ultimately causes some request to go out to be executed by the kernel while the calling thread is suspended by the scheduler/dispatcher until that work is complete. Typically this work is some kind of IO request to peripheral hardware (ie network card/hard disk/etc). The peripheral hardware can use a processor interrupt to signal the CPU/kernel that some piece of work is complete (or some other piece of info about the work). Once the work is complete, the kernel wakes up the calling thread and allows execution to continue from where it was waiting. The hardware/kernel together have prepared any outputs (buffers/errors etc) for the user-facing calling function and these outputs are handed back to the caller. The specific details of how this happens are going to be very different depending on the underlying OS. For a deep dive into how Windows works, I recommend: Scheduling, Thread Context, and IRQL. |
||||
|
|