-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
60 additions
and
11 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
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,26 @@ | ||
use wg::AsyncWaitGroup; | ||
use std::sync::Arc; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use tokio::{spawn, time::{sleep, Duration}}; | ||
|
||
#[tokio::main(flavor = "multi_thread", worker_threads = 10)] | ||
async fn main() { | ||
let wg = AsyncWaitGroup::new(); | ||
let ctr = Arc::new(AtomicUsize::new(0)); | ||
|
||
for _ in 0..5 { | ||
let ctrx = ctr.clone(); | ||
let t_wg = wg.add(1); | ||
spawn(async move { | ||
// mock some time consuming task | ||
sleep(Duration::from_millis(50)).await; | ||
ctrx.fetch_add(1, Ordering::Relaxed); | ||
|
||
// mock task is finished | ||
t_wg.done(); | ||
}); | ||
} | ||
|
||
wg.wait().await; | ||
assert_eq!(ctr.load(Ordering::Relaxed), 5); | ||
} |
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,26 @@ | ||
use wg::WaitGroup; | ||
use std::sync::Arc; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::time::Duration; | ||
use std::thread::{spawn, sleep}; | ||
|
||
fn main() { | ||
let wg = WaitGroup::new(); | ||
let ctr = Arc::new(AtomicUsize::new(0)); | ||
|
||
for _ in 0..5 { | ||
let ctrx = ctr.clone(); | ||
let t_wg = wg.add(1); | ||
spawn(move || { | ||
// mock some time consuming task | ||
sleep(Duration::from_millis(50)); | ||
ctrx.fetch_add(1, Ordering::Relaxed); | ||
|
||
// mock task is finished | ||
t_wg.done(); | ||
}); | ||
} | ||
|
||
wg.wait(); | ||
assert_eq!(ctr.load(Ordering::Relaxed), 5); | ||
} |
This file was deleted.
Oops, something went wrong.
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