spawn_on

Function spawn_on 

Source
pub fn spawn_on<F: Future + Send + 'static>(
    thread_name: &'static str,
    future: F,
)
Expand description

Spawns a future on a new thread and returns immediately without waiting for completion.

This function creates a new OS thread with the given name and runs the future on that thread using sleep_on. The calling thread returns immediately, making this useful for fire-and-forget tasks.

§Parameters

  • thread_name: The name to give to the spawned thread (must be a static string)
  • future: The future to execute on the new thread

§Requirements

  • The future must be Send because it will be moved to another thread
  • The future must be 'static because the spawned thread may outlive the caller

§Example

use test_executors::spawn_on;
use std::sync::{Arc, Mutex};
use std::time::Duration;

let data = Arc::new(Mutex::new(Vec::new()));
let data_clone = data.clone();

spawn_on("worker", async move {
    // Simulate some async work
    data_clone.lock().unwrap().push(42);
});

// Give the spawned thread time to complete
std::thread::sleep(Duration::from_millis(50));

// Check the result
assert_eq!(*data.lock().unwrap(), vec![42]);

§Panics

Panics if the thread cannot be spawned (e.g., due to resource exhaustion).

§See Also

  • spawn_local for a platform-aware version that works on WASM