Pitfalls of using std::sync::Mutex
with the multithreaded runtime
#6843
Unanswered
samueltardieu
asked this question in
General
Replies: 1 comment
-
Ultimately, it's perfectly fine to use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The documentation of
tokio::sync::Mutex
contains:This includes a well-articulated explanation of why this is the case. I concur with this explanation as long as the mutex is used within a single-threaded asynchronous runtime.
However, using
std::sync::Mutex
with a multithreaded executor may cause another thread to be temporarily blocked when the mutex is held. If this thread is running an asynchronous executor, it will be unable to execute other asynchronous tasks during this time, and other threads may not be available to execute these tasks through work stealing.If asynchronous tasks accessing this mutex are dispersed across the executor threads, there is a risk that multiple executor threads could become blocked. Shouldn't this be highlighted?
Beta Was this translation helpful? Give feedback.
All reactions