简体   繁体   English

阻塞和非阻塞读取有什么区别?

[英]What is the difference between a blocking and non-blocking read?

Add to the above question the concept of a wait/no wait indicator as a parameter to a ReadMessage function in a TCP/IP or UDP environment. 将等待/无等待指示符的概念添加到上述问题中作为TCP / IP或UDP环境中的ReadMessage函数的参数。

A third party function description states that: 第三方功能描述指出:

This function is used to read a message from a queue which was defined by a previous registerforinput call. 此函数用于从队列中读取消息,该队列由先前的registerforinput调用定义。 The input wait/no wait indicator will determine if this function will block on the queue specified, waiting for the data to be placed on the queue. 输入等待/无等待指示符将确定此函数是否将阻止指定的队列,等待数据放在队列中。 If the nowait option is specified and no data is available a NULL pointer will be returned to the caller. 如果指定了nowait选项且没有可用数据,则会将NULL指针返回给调用者。 When data available this function will return a pointer to the data read from the queue. 当可用数据时,此函数将返回指向从队列读取的数据的指针。

What does it mean for a function to be blocking or non-blocking? 函数阻塞或非阻塞是什么意思?

Blocking means that execution of your code (in that thread) will stop for the duration of the call. 阻止意味着您的代码(在该线程中)的执行将在调用期间停止。 Essentially, the function call will not return until the blocking operation is complete. 实质上,在阻塞操作完成之前,函数调用不会返回。

A blocking read will wait until there is data available (or a timeout, if any, expires), and then returns from the function call. 阻塞读取将等待,直到有可用数据(或超时,如果有的话,到期),然后从函数调用返回。 A non-blocking read will (or at least should) always return immediately, but it might not return any data, if none is available at the moment. 非阻塞读取将(或至少应该)立即返回,但如果当前没有数据,则可能不会返回任何数据。

An analogy if you'll permit me - sorry, it's late in the afternoon and I'm in the mood, if it gets down voted - ah well... 比喻如果你允许我 - 对不起,已经是下午很晚了,我心情很好,如果它被投票决定 - 啊......

You want to get into a snazzy nightclub, but the bouncer tells you you cannot go in till someone comes out. 你想进入一个时髦的夜总会,但保镖告诉你,在有人出来之前你不能进去。 You are effectively "blocked" on that condition. 在这种情况下你被有效地“阻止”了。 When someone comes out, you are free to go in - or some error condition such as "are those trainers?" 当有人出来时,你可以自由进入 - 或者出现一些错误状况,例如“那些训练师吗?” Your night doesn't really kick off till you get in, your enjoyment is "blocked". 在你进入之前,你的夜晚并没有真正开始,你的享受被“阻挡”了。

In a "non-blocking" scenario, you will tell the bouncer your phone number, and he will call you back when there is a free slot. 在“非阻塞”场景中,您将告知保镖您的电话号码,并且当有空闲插槽时他会给您回电。 So now you can do something else while waiting for someone to come out, you can start your night somewhere else and come back when called and continue there... 所以现在你可以在等待别人出来的时候做点别的事,你可以在其他地方开始你的夜晚,然后在被叫的时候回来继续......

Sorry if that didn't help... 对不起,如果这没有帮助...

Take a look at this: http://www.scottklement.com/rpg/socktut/nonblocking.html 看看这个: http//www.scottklement.com/rpg/socktut/nonblocking.html

Here's some excerpts from it: 以下是一些摘录:

  • 'By default, TCP sockets are in "blocking" mode. '默认情况下,TCP套接字处于“阻塞”模式。 For example, when you call recv() to read from a stream, control isn't returned to your program until at least one byte of data is read from the remote site. 例如,当您调用recv()从流中读取时,控件不会返回到您的程序,直到从远程站点读取至少一个字节的数据。 This process of waiting for data to appear is referred to as "blocking".' 等待数据出现的这个过程称为“阻塞”。

  • 'Its possible to set a descriptor so that it is placed in "non-blocking" mode. '可以设置描述符,使其处于“非阻塞”模式。 When placed in non-blocking mode, you never wait for an operation to complete. 当处于非阻塞模式时,您永远不会等待操作完成。 This is an invaluable tool if you need to switch between many different connected sockets, and want to ensure that none of them cause the program to "lock up."' 如果您需要在许多不同的连接套接字之间切换,并且希望确保它们都不会导致程序“锁定”,这是一个非常宝贵的工具。

Also, it's generally a good idea to try to search for an answer first (just type "blocking vs. non-blocking read" in a search engine), and then once you hit a wall there to come and ask questions that you couldn't find an answer to. 此外,尝试首先搜索答案通常是一个好主意(只需在搜索引擎中键入“阻止与非阻塞读取”),然后一旦你碰到那里的墙就会问你不能问的问题。找到答案。 The link I shared above was the second search result. 我上面分享的链接是第二个搜索结果。 Take a look at this great essay on what to do before asking questions on internet forums: http://www.catb.org/~esr/faqs/smart-questions.html#before 在互联网论坛上提问之前,先看一下这篇关于该怎么做的精彩文章: http//www.catb.org/~esr/faqs/smart-questions.html#before

In your case, it means the function will not return until there actually is a message to return. 在您的情况下,这意味着在实际返回消息之前,函数不会返回。 It'll prevent your program from moving forward, but when it does move forward you'll have a message to work with. 它会阻止你的程序继续前进,但是当它向前推进时你会有一个消息可以使用。

If you specify nowait , a null pointer will be returned immediately if there are no messages on the queue, which allows you to process that situation. 如果指定nowait ,如果队列中没有消息,则会立即返回空指针,这允许您处理该情况。

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

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