本文介绍了如何在Task.Wait中出现死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在CLR中通过C#book提到


"当一个线程调用Wait方法时, 系统检查线程正在等待的任务是否已开始执行。如果有,则调用Wait的线程将阻塞,直到Task完成运行。但是如果Task还没有启动
执行,那么系统可以(取决于TaskScheduler)通过使用名为Wait"的线程执行任务。


"如果线程在调用Wait之前已经进行了线程同步锁定,然后Task尝试采用相同的锁定,从而导致死锁线程!"



我无法理解第二部分,如何发生死锁。它的任务是在同一个线程上执行,然后任务应该能够获取锁,因为同一个线程已经锁定。有人可以用简单的示例解释死锁是如何发生的吗



谢谢

解决方案

Hi,

in CLR via C# book it is mentioned that

"When a thread calls the Wait method, the  system checks if the Task that the thread is waiting for has started executing. If it has, then the thread calling Wait will block until the Task has completed running. But if the Task has not started executing yet, then the system may (depending on the TaskScheduler) execute the Task by using the thread that called Wait".

"if the thread has taken a thread synchronization lock before calling Wait and then the Task tries to take the same lock, resulting in a deadlocked thread!"

I am not able to understand the second part, how deadlock can happen. it the task is executing on the same thread, then task should be able to acquire lock, as the same thread has taken lock. Can somebody pls explain with simple sample how deadlock can happen

Thanks

解决方案


这篇关于如何在Task.Wait中出现死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:26