-
As the doc of Lines 26 to 27 in 5ea3c63 And since tokio/tokio/src/task/blocking.rs Lines 4 to 5 in 5ea3c63 which is suitable for synchronous contexts: tokio/tokio/src/task/blocking.rs Lines 23 to 25 in 5ea3c63 We have two ways to lock a blocking mutex:
The second approach may be better as it avoids blocking the executor. Do I understand this correctly? Thanks for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
When using a blocking mutex, you must ensure that the call to Using |
Beta Was this translation helpful? Give feedback.
When using a blocking mutex, you must ensure that the call to
lock
takes a short amount of time. And if it does that, it's not really blocking.Using
block_in_place
is a bad idea. The way it works is that a new worker thread is spawned in the background. It's not a magic bullet and it can be really expensive. If locking the mutex takes a short amount of time, then it is unnecessary. If locking the mutex takes a long time, you should usetokio::sync::Mutex
instead ofblock_in_place
.