Tokio which function safe to call in not tokio thread?

It is obvious that async function I should call inside of tokio threads via tokio::...::block_on or tokio::...::spawn. But what about other functions, in particular I am intereting about Notify::new, Notify::notify_one, oneshot::channel and oneshot::Sender::send.

Is it safe to call this functions after "tokio runtime" was created by in thread not related to tokio:

let runtime = Builder::new_multi_thread()
let handle = runtime.handle().clone();
std::thread::spawn(move || {
  let (tx, rx) = oneshot::channel();
  handle.spawn(async { 
     // run inside tokio controlled threads
     work_with(&mut rx);
  // run inside normal thread not related to tokio
  work_with_tx(&mut tx);


Generally, things that require a runtime are the things that need to use its IO or timer driver. The things in tokio::sync will work anywhere.

"Safe" has a very particular meaning in the context of Rust: it means "memory-safe" (i.e., free of common memory management errors such as use-after-free or race conditions). If there is no unsafe code involved, and the code you posted compiles, then it should be safe (modulo potential bugs in any eventual unsafe code in the libraries you are relying on).

Is this what you mean?

I think OP means safe as in doesn't panic or hang when the current thread doesn't have a tokio runtime.

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.