当一个线程被阻塞时,会导致同一进程或整个进程中另一个线程的阻塞吗?
当一个线程被阻塞时,该线程是否有必要阻塞同一进程或其进程中的任何其他线程?每次都会发生这种情况吗?
when a thread is being blocked is it necessary that this thread blocks any other threads in the same process or its process?is this happening every time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。如果不是这样的话,我们就不必应对僵局。这种情况是被阻塞的线程获取了另一个线程也尝试获取的同步对象。它会阻塞。
好的,我现在明白问题了。一般来说,是的。需要运行一些其他代码来释放阻塞条件。非明显的情况是,如果线程在 I/O 上被阻塞,则内核线程会在驱动程序中运行代码。或者线程调度程序,以防线程由于正在等待获取处理器或正在等待非无限超时而被阻塞。
Sure. We wouldn't have to cope with deadlock if that wasn't the case. The scenario is that the blocked thread acquired a synchronization object that another thread tries to acquire as well. It will block.
Okay, I get the question now. Generally, yes. Some other code needs to run to release the blocking condition. The non-obvious cases are kernel threads that run code in drivers if the thread is blocked on I/O. Or the thread scheduler, in case the thread is blocked because it is waiting to acquire the processor or is waiting with a non-infinite time-out.
是的,任何同步操作都可能发生这种情况,其中一个线程需要等待另一个线程执行某些操作。例如,如果线程 A 持有锁,然后在内核中执行长时间阻塞操作,则另一个线程 B 如果尝试获取该锁,就会阻塞。线程 B 至少会被阻塞,直到线程 A 的阻塞操作完成。
在极端情况下,线程相互等待将导致死锁
额外说明:
进程中的线程彼此独立运行。一个线程阻塞不一定会阻塞其他线程。这是首先使用线程的主要原因之一。
Yes, this can happen for any synchronization operation, where one thread needs to wait for another thread to do something. For instance, if thread A holds a lock and then does a long blocking operation in the kernel, another thread B will block if it tries to acquire the lock. Thread B will be blocked at least until thread A's blocking operation finishes.
In extreme cases, threads waiting on each other will result in Deadlock
Additional clarification:
Threads in a process run independently of each other. One thread blocking shouldn't necessarily block the other threads. This is one of the main reasons that threads are used in the first place,.
当一个线程被阻塞时,它也会阻塞进程内的所有线程
when a thread is blocked , it blocks also all the threads within the process