Help: passing lock objects to spawned tasks leads to deadlock #6253
-
This code uses a It is expected that using However, in the Code-A
Code-A output:
After some trials, I also found that if I launch another task ( Code-B
Code-B output:
I'm not sure why it behaves this way. Is it due to my misunderstanding or incorrect usage? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The Do not using a never return( or never yield) blocking code in tokio runtime! If you are looking for a solution for this kind of use, you can refer to https://ryhl.io/blog/async-what-is-blocking/. |
Beta Was this translation helpful? Give feedback.
You do not have to know the detail of task switching, but you would better know use tokio::task::spawn_blocking function to run your
loop{}
. This is one of the key point of https://ryhl.io/blog/async-what-is-blocking/.Your problem is not dealock issue.