rumtk_init_threads

Macro rumtk_init_threads 

Source
macro_rules! rumtk_init_threads {
    ( ) => { ... };
    ( $threads:expr ) => { ... };
}
Expand description

First, let’s make sure we have tokio initialized at least once. The runtime created here will be saved to the global context so the next call to this macro will simply grab a reference to the previously initialized runtime.

Passing nothing will default to initializing a runtime using the default number of threads for this system. This is typically equivalent to number of cores/threads for your CPU.

Passing threads number will yield a runtime that allocates that many threads.

§Examples

    use rumtk_core::{rumtk_init_threads, rumtk_resolve_task, rumtk_create_task_args, rumtk_create_task, rumtk_spawn_task};
    use rumtk_core::core::RUMResult;
    use rumtk_core::threading::thread_primitives::SafeTaskArgs;

    async fn test(args: &SafeTaskArgs<i32>) -> RUMResult<Vec<i32>> {
        let mut result = Vec::<i32>::new();
        for arg in args.read().await.iter() {
            result.push(*arg);
        }
        Ok(result)
    }

    let rt = rumtk_init_threads!();                                      // Creates runtime instance
    let args = rumtk_create_task_args!(1);                               // Creates a vector of i32s
    let task = rumtk_create_task!(test, args);                           // Creates a standard task which consists of a function or closure accepting a Vec<T>
    let result = rumtk_resolve_task!(&rt, rumtk_spawn_task!(&rt, task)); // Spawn's task and waits for it to conclude.
    use rumtk_core::{rumtk_init_threads, rumtk_resolve_task, rumtk_create_task_args, rumtk_create_task, rumtk_spawn_task};
    use rumtk_core::core::RUMResult;
    use rumtk_core::threading::thread_primitives::SafeTaskArgs;

    async fn test(args: &SafeTaskArgs<i32>) -> RUMResult<Vec<i32>> {
        let mut result = Vec::<i32>::new();
        for arg in args.read().await.iter() {
            result.push(*arg);
        }
        Ok(result)
    }

    let thread_count: usize = 10;
    let rt = rumtk_init_threads!(&thread_count);
    let args = rumtk_create_task_args!(1);
    let task = rumtk_create_task!(test, args);
    let result = rumtk_resolve_task!(&rt, rumtk_spawn_task!(&rt, task));