描述:
下面的内容源自 Rust Async Book:Async Await,不明白下面的描述中为什么会造成deadlock?
Similarly, it isn't a good idea to hold a traditional non-futures-aware lock across an .await, as it can cause the threadpool to lock up: one task could take out a lock, .await and yield to the executor, allowing another task to attempt to take the lock and cause a deadlock. To avoid this, use the Mutex in futures::lock rather than the one from std::sync.
理解的时候会顺着上面的意思进行理解: Task1 获取锁, 然后Task1 执行 await让出执行器给其他Tasks使用,other Tasks 尝试获取该锁但是会被阻塞因为该锁被其他task获取了,这样当前执行器会一直被占用 造成死锁;
但是:
上面的理解让自己是懂非懂,比如:
Task1 获取锁, 然后Task1 执行 await让出执行器给其他Tasks使用,other Tasks 尝试获取该锁但是会被阻塞因为该锁被其他task获取了,这样当前执行器会一直被占用; 这个时候,如果task1唤醒后被其他的执行器执行并完全退出,那这锁不就解开了吗?
期望
- 给出自己的理解
- 尽可能给出参考链接,或者其他有说服力的资料,
感谢在先
。
lock -> await / yield ,await 会把控制权交出去,但是锁没有释放。然后后面别人再 lock ,就可能死锁了。