-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
can discover which future was executed before task halted
- Loading branch information
Showing
4 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Note that this example will not finish by itself and almost guaranteed to deadlock. | ||
use std::sync::Arc; | ||
|
||
use tokio::sync::Mutex; | ||
use tracing::instrument; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
tracing_stacks::init(); | ||
|
||
let state = Arc::new(State { | ||
first: Mutex::new(0), | ||
second: Mutex::new(0), | ||
}); | ||
let handle1 = tokio::spawn({ | ||
let state = state.clone(); | ||
async move { | ||
loop { | ||
state.increment_in_order().await; | ||
} | ||
} | ||
}); | ||
let handle2 = tokio::spawn({ | ||
let state = state.clone(); | ||
async move { | ||
loop { | ||
state.increment_out_of_order().await; | ||
} | ||
} | ||
}); | ||
handle1.await.expect("no error"); | ||
handle2.await.expect("no error"); | ||
} | ||
|
||
struct State { | ||
first: Mutex<u64>, | ||
second: Mutex<u64>, | ||
} | ||
|
||
impl State { | ||
#[instrument(skip(self))] | ||
async fn increment_in_order(&self) { | ||
let mut first = self.first.lock().await; | ||
let mut second = self.second.lock().await; | ||
*first += 1; | ||
*second += 1; | ||
} | ||
|
||
#[instrument(skip(self))] | ||
async fn increment_out_of_order(&self) { | ||
let mut second = self.second.lock().await; | ||
let mut first = self.first.lock().await; | ||
*first += 1; | ||
*second += 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters