2022-02-15 18:20:16 +00:00
|
|
|
use jitterbug::{oneshot, Executor, JoinError};
|
2022-01-30 02:26:35 +00:00
|
|
|
|
|
|
|
fn main() -> Result<(), JoinError> {
|
|
|
|
let executor = Executor::new();
|
|
|
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
let executor = executor.clone();
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
let _ = executor.block_on(std::future::pending::<()>());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let execu2r = executor.clone();
|
|
|
|
executor.block_on(async move {
|
|
|
|
let mut join_handles = Vec::new();
|
|
|
|
for _ in 0..4 {
|
2022-01-30 02:59:19 +00:00
|
|
|
join_handles.extend((0..502).flat_map(|i| {
|
|
|
|
let (tx, rx) = oneshot();
|
|
|
|
|
|
|
|
[
|
|
|
|
execu2r.spawn(async move {
|
|
|
|
let _ = tx.send(i);
|
|
|
|
}),
|
|
|
|
execu2r.spawn(async move {
|
|
|
|
if let Ok(t) = rx.await {
|
|
|
|
println!("{:?} - {}", std::thread::current().id(), t);
|
|
|
|
} else {
|
|
|
|
println!("{:?} - oops", std::thread::current().id());
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
}));
|
2022-01-30 02:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for handle in join_handles {
|
|
|
|
handle.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Done waiting");
|
|
|
|
|
|
|
|
Ok(()) as Result<(), JoinError>
|
|
|
|
})?
|
|
|
|
}
|