You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
setpill opened this issue
Mar 12, 2021
· 2 comments
· Fixed by #3615
Labels
A-tokioArea: The main tokio crateC-bugCategory: This is a bug.E-easyCall for participation: Experience needed to fix: Easy / not muchE-help-wantedCall for participation: Help is requested to fix this issue.M-syncModule: tokio/syncT-docsTopic: documentation
/// Tokio's Mutex works in a simple FIFO (first in, first out) style where all
/// calls to [`lock`] complete in the order they were performed. In that way the
/// Mutex is "fair" and predictable in how it distributes the locks to inner
/// data. This is why the output of the program above is an in-order count to
/// 50. Locks are released and reacquired after every iteration, so basically,
This does not line up. The in-order count seems to be a consequence of the println happening before the lock is released. If Tokio's Mutex worked in a FILO order it would not count backwards.
The text was updated successfully, but these errors were encountered:
If you modify it like this, you would see the effect of the fairness though:
for i in0..5{let my_count = Arc::clone(&count);
tokio::spawn(asyncmove{for _ in0..10{letmut lock = my_count.lock().await;*lock += 1;println!("{} {}", i, lock);}});}
Darksonn
added
E-easy
Call for participation: Experience needed to fix: Easy / not much
E-help-wanted
Call for participation: Help is requested to fix this issue.
M-sync
Module: tokio/sync
T-docs
Topic: documentation
labels
Mar 12, 2021
The ordering changes between runs, and it definitely is not guaranteed to happen in source code order, that's for sure 😄 I think it would be better to substitute "predictable" with "deterministic".
Edit: upon closer inspection, it seems that the unpredictability in the order mostly comes from the start where some threads already start running before the others are set up.
A-tokioArea: The main tokio crateC-bugCategory: This is a bug.E-easyCall for participation: Experience needed to fix: Easy / not muchE-help-wantedCall for participation: Help is requested to fix this issue.M-syncModule: tokio/syncT-docsTopic: documentation
tokio/tokio/src/sync/mutex.rs
Lines 66 to 92 in a1b4bde
tokio/tokio/src/sync/mutex.rs
Lines 100 to 104 in a1b4bde
This does not line up. The in-order count seems to be a consequence of the
println
happening before the lock is released. If Tokio's Mutex worked in a FILO order it would not count backwards.The text was updated successfully, but these errors were encountered: