Skip to content

Commit

Permalink
rt: add a method to determine if the runtime is multi-threaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
agayev committed Oct 27, 2022
1 parent 32d68fe commit bb99d33
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tokio/src/runtime/handle.rs
Expand Up @@ -101,6 +101,33 @@ impl Handle {
}
}

/// Returns true if the current `Runtime` is multi-threaded and false
/// otherwise.
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// assert!(!Handle::current().is_multi_threaded());
/// }
/// ```
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "multi_thread", worker_threads = 4)]
/// async fn main() {
/// assert!(Handle::current().is_multi_threaded());
/// }
/// ```
pub fn is_multi_threaded(&self) -> bool {
match self.inner {
scheduler::Handle::CurrentThread(_) => false,
scheduler::Handle::MultiThread(_) => true,
}
}

/// Returns a Handle view over the currently running Runtime
///
/// Returns an error if no Runtime has been started
Expand Down

0 comments on commit bb99d33

Please sign in to comment.