pub struct TaskGroup { /* private fields */ }
Expand description
Group of tasks to be waited on.
Implementations§
Source§impl TaskGroup
impl TaskGroup
Sourcepub fn add(&self)
pub fn add(&self)
Increases the task counter by 1.
This is used to indicate an intention for an upcoming task.
Call to this function should be matched by call to Self::done
.
If the call to done() needs to be done in a RAII manner use Self::add_work
Sourcepub fn add_n(&self, n: u32)
pub fn add_n(&self, n: u32)
Increases the task counter by n
.
This is used to indicate an intention for an upcoming task.
Call to this function should be matched by call to Self::done
.
If the call to done() needs to be done in a RAII manner use Self::add_work
Sourcepub fn add_work(&self, n: u32) -> Work
pub fn add_work(&self, n: u32) -> Work
Creates a work which does increment the task counter by n
.
The returned Work
decrements the counter when dropped.
Sourcepub fn wait(&self) -> WaitFuture ⓘ
pub fn wait(&self) -> WaitFuture ⓘ
Returns the WaitFuture
When awaited the future returns the internal counter, which can be 0 or -ve.
A value can be -ve if there were more TaskGroup::done
calls than TaskGroup::add
.
The caller can use this to maintain an invariant in case of any mis-behaving tasks.
The future when resolved also resets the internal counter to zero. The taskgroup then can be reused.
use taskwait::TaskGroup;
#[tokio::main]
async fn main() {
let tg = TaskGroup::new();
// ... Spawn tasks ...
let n = tg.wait().await;
if n < 0 {
// Return Error
}
}