2

At my multithread application I using AF_UNIX type of socket. Seems by default its blocking. The question is whats mean by "blocking"? Is it block the thread when it execute the ::recv or ::send calls or all threads on the application (like fgets do)?

If it block all threads/whole application, I guess need to use non-blocking sockets? If so, please, give the good example of how to set up AF_UNIX non-blocking socket and when need to set non-blocking mode (and how). How to ::recv ? thanks.

abrahab
  • 2,430
  • 9
  • 39
  • 64
  • Think about it for a minute. If sockets blocks the whole application, then the application literally won't be able to do anything while it's waiting on a socket. That alone would make blocking sockets damn near useless, if they actually blocked the entire application. – In silico Jun 09 '12 at 13:30
  • @Insilico The question originated from `popen` + `fgets` problem. `fgets` block all my threads on call from only one thread. Therefore, I thought that this may perhaps also by blocking sockets while the big amount of data will be transmitted. – abrahab Jun 09 '12 at 13:39
  • I think you're mistaken. `fgets()` doesn't block your entire application either. In fact IIRC there isn't a single function I know of where "blocking" means "blocks the entire application". Perhaps something else caused the entire application to look like it's blocked (e.g. a deadlock). – In silico Jun 09 '12 at 13:51
  • @Insilico in this thread http://stackoverflow.com/questions/10960622/popen-pipe-slows-down-other-threads I have currently not solved problem about this, and, right now, the only answer is that its `fgets`. – abrahab Jun 09 '12 at 14:13
  • 1
    I doubt the validity of the answer in your other question and left a comment stating so. I think that fgets() should only block the current thread. – Brady Jun 09 '12 at 14:45
  • @Brady thanks, lets discuss this at the special topic http://stackoverflow.com/questions/10960622/popen-pipe-slows-down-other-threads where I get the answer that `fgets` block my app. EDIT: Ohhh, sorry, got it, you already send the answer to this topic. – abrahab Jun 09 '12 at 14:48
  • 1
    @abrahab, I just posted an answer on your other question, hopefully that will help. – Brady Jun 09 '12 at 15:02

2 Answers2

5

Blocking calls make the thread wait for the operation to complete. Use them when your thread cannot continue before the operation has completed, for example due to the data dependency on the input being received.

Non-blocking calls return as soon as the information is buffered for transmission, or the read operation is initiated. Use them when there are no data dependencies.

In general, blocking always means "blocks the current thread", not "block all threads in my process.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

It only blocks the thread that makes the recv call.

Superman
  • 3,027
  • 1
  • 15
  • 10