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

13,564

Solution 1

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.

Solution 2

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...

Solution 3

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

Here's some excerpts from it:

  • 'By default, TCP sockets are in "blocking" mode. 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. 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

Share:
13,564
user553514
Author by

user553514

Updated on June 04, 2022

Comments

  • user553514
    user553514 almost 2 years

    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.

    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. 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. 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?

  • user553514
    user553514 about 13 years
    I am inside one thread I created and I do want to only read from a queue when data is available , otherwise when there is no data I would like the processor to check the other threads (for example the main one from which I created this thread). Which one do I use? blocking or non-blocking option?
  • user553514
    user553514 about 13 years
    I am inside one thread I created and I do want to only read from a queue when data is available , otherwise when there is no data I would like the processor to check the other threads (for example the main one from which I created this thread)
  • Nim
    Nim about 13 years
    @Downvoter.. okay what's wrong with my analogy? The OP did ask what is "blocking" vs "non-blocking"...
  • user553514
    user553514 about 13 years
    I did go to internet but somehow I was not hitting anything directly related or useful. Stack Overflow enjoys a bounty of great minds. So I only ask here if I don't succeed on my google searches. Thank you for your reply.
  • Steve Jessop
    Steve Jessop about 13 years
    @user553514: "block" means that this thread of execution blocks. Other threads can still be scheduled while this thread is blocking.
  • user553514
    user553514 about 13 years
    @SteveJessop: Thank you for clarifying who is being blocked. That was my main confusion.
  • J. Taylor
    J. Taylor about 13 years
    No worries. Anyhow, I hope the answer above was helpful. Take a look at the essay I linked to, also -- it's a great read.
  • supercat
    supercat about 11 years
    For a "blocking" scenario, the bouncer wouldn't let you leave if it turned out the club was full; you'd have no alternative but to wait. For a "non-blocking" scenario, you could ask the bouncer if there was space, and would be free to leave in the hopes of coming back later when there is space, but successful admission would require showing up at the moment a space exists. The "asynchronous I/O" scenario is the one where you leave your number with the bouncer.