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

source

pub async fn new(handlers: usize) -> Self

source

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
Hide additional 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 Drop for Context

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl TaskContext for Context

source§

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

Creates an asynchronous task used by [tasc::task::Handle] and [tasc::task::BlockingHandle].

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.