Skip to content

The best practice for using the blocking mutex in the asynchronous context #6785

Answered by Darksonn
vvvviiv asked this question in Q&A
Discussion options

You must be logged in to vote

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 use tokio::sync::Mutex instead of block_in_place.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@vvvviiv
Comment options

Answer selected by vvvviiv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants