简体   繁体   中英

What happens to other thread when one thread get blocked?

In Linux, if two threads are created and both of them are running, when one of them calls recv() or any IO syscall that blocks when no data is available, what would happen to the whole process?

Will the other thread block also? I guess this depends on how threading is implemented. If thread library is in user space and kernel totally unaware of the threads within process, then process is the scheduling entity and thus both threads got blocked.

Further, if the other thread doesn't block because of this , can it then send() data via the same socket, which is blocking the recv thread? Duplexing?

Any ideas?

Blocking calls in one thread should not affect other threads.

If the blocked thread locks a mutex prior to entering the blocked call and the second thread attempts to lock the same mutex, then they the second thread would need to wait for the blocking call to finish and for the first thread to release the lock.

It's completely possible to implement threads in user-space such that one thread can proceed while another thread block on I/O.

The non-blocked thread should be able to send on the socket while the other thread is blocking on it (I've written such code).

You're absolutely right that the blocking behavior will depend on if the thread is implemented in kernel space, or in user space. If threading is implemented purely in user space (that is, the kernel is completely uninvolved with the threading), then any blocking entry point into the kernel will need to be wrapped with some non-blocking variant that can simulate blocking semantics to its calling "thread" (eg using AIO to send / recv data instead of blocking, and the completion callback makes the thread runnable, again).

In Linux (and every other extant major OS I can think of), threading is implemented at the kernel level, or similar, and a blocking call into the kernel will not cause all other threads to block.

Yes, you can send() to a socket for which another thread is blocked on recv() .

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