How to solve thread sync

i have a function to launch 2 thread, and i want to block them.

pub fn run(self) {
    let get_info_task = self.executor.clone().spawn(async move {
        doSomeThing();
    });
    let send_nonce_task = self.executor.clone().spawn(async move {
        doSomeThing();
    });
    join_all(vec![get_info_task,send_nonce_task]).await;
}

the self.executor is builded by

let rt = Builder::new()
        .core_threads(1)
        .threaded_scheduler()
        .enable_all()
        .build()
        .unwrap();
rt.handle().clone()

but if i do like this

513 | |         join_all(vec![get_info_task,send_nonce_task]).await;
    | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ only allowed inside `async` functions and blocks

if i add

#[tokio::main]
async

it will throws out that

can't use runtime within runtime

do everybody know how to block them? because if i don't use join the main thread will break out.
does block_on helpful???

You don't need join_all if you are also using spawn.

pub fn run(self) {
    let (a, b) = self.executor.block_on(async move {
        let a = tokio::spawn(async move {
            doSomeThing();
        });
        let b = tokio::spawn(async move {
            doSomeThing();
        });
        (a.await.unwrap(), b.await.unwrap())
    });
    
    println!("{} {}", a, b);
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.