简体   繁体   中英

What does blocking mode mean?

I can't seem to find a useful definition for "blocking" (or for that matter "non-blocking") when used in relation to POSIX C functions.

For example read() may be called in blocking or non-blocking mode on a FIFO pipe. If called in blocking mode, it will block until it's opened elsewhere for writing.

Will this blocking just seize up the thread? Or the process? Or will it pause the rendering of the multiverse?

Blocking means that the thread is de-scheduled off the CPU while waiting for an event to happen. When a thread is de-scheduled it doesn't consume any CPU cycles and allows other threads to make progress or put the CPU in a lower power state if there are no other threads waiting to run.

One thread blocking doesn't affect other threads you may have in the process. A blocking call only blocks the calling thread.

For example, read blocks when there is no data in the pipe to read. When data arrives it "unblocks" and the read call returns.

In the kernel each file description and other objects one can block on (eg mutex or condition_variable ) have a list of waiting threads. When a thread blocks on an object it is appended to that object's wait list and de-scheduled off the CPU. Whenever an event for the object occurs the kernel checks the wait list for waiting threads for such an event and if there are any one or multiple threads get scheduled again and the blocking calls eventually return.

In non-blocking mode such calls do not block but return immediately an error code with errno being set to EWOULDBLOCK or EAGAIN , which are nowadays two different names for the same errno value. (pthread calls do not set errno but return the error value directly).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM