-
i want to spawn a backgroud runtime(current_thread) to resolve some events, the only usage is getting the handle and call spawn to create a task, the code will be much more simpler if we have fn main() {
static TASK_HANDLER: AtomicPtr<Handle> = AtomicPtr::new(std::ptr::null_mut());
let (created, is_created) = oneshot::channel();
std::thread::spawn(|| {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
TASK_HANDLER.store(
Box::into_raw(Box::new(rt.handle().clone())),
std::sync::atomic::Ordering::Relaxed,
);
created.send(());
// this will block the thread until the runtime is closed
rt.run_forever();
});
is_created.blocking_recv().unwrap();
// ...
// push tasks
unsafe {
TASK_HANDLER
.load(std::sync::atomic::Ordering::Relaxed)
.as_ref()
.unwrap()
}
.spawn(async {
//...
});
} |
Beta Was this translation helpful? Give feedback.
Answered by
Darksonn
Jan 16, 2025
Replies: 1 comment 1 reply
-
You can use |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ogios
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
rt.block_on(std::future::pending())