Struct tasc::StdContext
source · pub struct StdContext { /* private fields */ }Expand description
The default context using crossbeam and the standard library.
This context facilitates creating new tasks and effectively dividing them among workers via a wait queue.
Creating new threads uses the Rust standard library [std::thread::spawn] with its default settings.
Implementations§
source§impl Context
impl Context
pub async fn new(handlers: usize) -> Self
sourcepub fn new_blocking(handlers: usize) -> Self
pub fn new_blocking(handlers: usize) -> Self
Examples found in repository?
examples/blocking.rs (line 93)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
fn local() -> Result<(), Box<dyn std::error::Error>> {
type TaskBuilder<'a> = tasc::TaskBuilder<'a, tasc::StdContext, tasc::StdSignal>;
let ctx = tasc::StdContext::new_blocking(100);
let task0 = TaskBuilder::from_ctx(&ctx).spawn_blocking(|_id| {
println!("task0");
std::thread::sleep(Duration::from_secs(3));
let mut i = 0u128;
#[allow(clippy::unit_arg)]
std::hint::black_box(for _ in 0..100000 {
i += 1;
});
println!("task 0 finished");
i
});
let task1 = TaskBuilder::from_ctx(&ctx).spawn_blocking(|_id| {
println!("task1");
let mut i = 0u128;
#[allow(clippy::unit_arg)]
std::hint::black_box(for _ in 0..100000 {
i += 1;
});
println!("task 1 finished");
i
});
let task2 = TaskBuilder::from_ctx(&ctx).spawn_blocking(|_id| {
println!("task2");
let mut i = 0u128;
#[allow(clippy::unit_arg)]
std::hint::black_box(for _ in 0..100000 {
i += 1;
});
println!("task 2 finished");
i
});
println!("task0: {}", task0.wait()?);
println!("task1: {}", task1.wait()?);
println!("task2: {}", task2.wait()?);
Ok(())
}More examples
examples/stress.rs (line 142)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
fn perform_tasks(workers: usize, work: usize) -> (f64, f64) {
println!("workers: {workers}, work: {work}");
let ctx = tasc::StdContext::new_blocking(workers);
let start = Arc::new(AtomicBool::new(false));
let mut handlers = Vec::with_capacity(workers);
let work_per_worker = work / workers;
let mut remainder = work % workers;
for _ in 0..workers {
let worker_load = work_per_worker + if remainder > 0 { 1 } else { 0 };
let start = start.clone();
handlers.push(
tasc::TaskBuilder::<_, tasc::global::Signal>::from_ctx(&ctx).spawn_blocking(
move |_| {
while !start.load(Ordering::Acquire) {
// we want it to busy wait
std::hint::black_box(())
}
let start = Instant::now();
for _ in 0..worker_load {
#[allow(clippy::unit_arg)]
std::hint::black_box(expensive());
}
Instant::now() - start
},
),
);
remainder = remainder.saturating_sub(1);
}
let total_time = Instant::now();
start.store(true, Ordering::Release);
let all_workers_time = handlers
.into_iter()
.map(|h| h.wait().unwrap().as_secs_f64())
.sum::<f64>();
let total_time = (Instant::now() - total_time).as_secs_f64();
let avg_time = all_workers_time / workers as f64;
println!("total time: {total_time:.02}, avg time: {avg_time:.05}\n");
(total_time, avg_time)
}Trait Implementations§
source§impl TaskContext for Context
impl TaskContext for Context
source§async fn set_limit(&self, max: usize)
async fn set_limit(&self, max: usize)
The limit can only ever increase.
If you set the limit, then set it again to a lower limit,
the limit will not change.
source§async fn create_task(&self, f: TaskFn) -> ComHandle
async fn create_task(&self, f: TaskFn) -> ComHandle
Creates an asynchronous task used by
[tasc::task::Handle] and [tasc::task::BlockingHandle].Auto Trait Implementations§
impl !Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more