简体   繁体   English

阻塞模式是什么意思?

[英]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.当与 POSIX C 函数相关时,我似乎找不到“阻塞”(或就此而言“非阻塞”)的有用定义。

For example read() may be called in blocking or non-blocking mode on a FIFO pipe.例如,可以在 FIFO 管道上以阻塞或非阻塞模式调用read() 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.阻塞意味着线程在等待事件发生时从 CPU 中取消调度。 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.当一个线程被取消调度时,它不会消耗任何 CPU 周期,并且如果没有其他线程等待运行,则允许其他线程取得进展或将 CPU 置于低功耗状态。

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.例如,当管道中没有要读取的数据时read块。 When data arrives it "unblocks" and the read call returns.当数据到达时,它“解除阻塞”并且read调用返回。

In the kernel each file description and other objects one can block on (eg mutex or condition_variable ) have a list of waiting threads.在内核中,每个文件描述和其他可以阻塞的对象(例如mutexcondition_variable )都有一个等待线程列表。 When a thread blocks on an object it is appended to that object's wait list and de-scheduled off the CPU.当一个线程在一个对象上阻塞时,它会被附加到该对象的等待列表中并从 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.在非阻塞模式下,此类调用不会阻塞,而是立即返回错误代码,其中errno被设置为EWOULDBLOCKEAGAIN ,现在它们是同一个errno值的两个不同名称。 (pthread calls do not set errno but return the error value directly). (pthread 调用不设置errno而是直接返回错误值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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