Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a thread waiting on IO also block a core?

In the synchronous/blocking model of computation we usually say that a thread of execution will wait (be blocked) while it waits for an IO task to complete.

My question is simply will this usually cause the CPU core executing the thread to be idle, or will a thread waiting on IO usually be context switched out and put into a waiting state until the IO is ready to be processed?

like image 657
JamieP Avatar asked Feb 28 '16 21:02

JamieP


People also ask

Do waiting threads consume CPU?

None. See above, but the CPU overhead used to manage the threads does not change based on the thread context.

Does a blocking thread use CPU?

Suspended or blocked thread do not consume any CPU time.

Why do threads block on Io?

Thread gets blocked just because when two thread try to access the single resource simultaneously the threads get blocked and when one thread is using the resource other should be put to sleep state because when the thread which is in processing produces the resullt only then the resource is allowed to another thread.

How does non blocking IO work?

Non-blocking IO under the hood Most non-blocking frameworks use an infinite loop that constantly checks (polls) if data is returned from IO. This is often called the event loop. An event loop is literally a while(true) loop that in each iteration will check if data is ready to read from a network socket.


1 Answers

A CPU core is normally not dedicated to one particular thread of execution. The kernel is constantly switching processes being executed in and out of the CPU. The process currently being executed by the CPU is in the "running" state. The list of processes waiting for their turn are in a "ready" state. The kernel switches these in and out very quickly. Modern CPU features (multiple cores, simultaneous multithreading, etc.) try to increase the number of threads of execution that can be physically executed at once.

If a process is I/O blocked, the kernel will just set it aside (put it in the "waiting" state) and not even consider giving it time in the CPU. When the I/O has finished, the kernel moves the blocked process from the "waiting" state to the "ready" state so it can have its turn ("running") in the CPU.

So your blocked thread of execution blocks only that: the thread of execution. The CPU and the CPU cores continue to have other threads of execution switched in and out of them, and are not idle.

like image 89
e0k Avatar answered Sep 24 '22 04:09

e0k