If my code has:
#[tokio::main]
async fn main() {
// mutates a global read-only variable "unsafe"
}
Will that mutation on the global read-only variable happen before or after Tokio sets up its thread pool?
From the tokio documentation:
#[tokio::main] async fn main() { println!("Hello world"); }
Equivalent code not using
#[tokio::main]
fn main() { tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(async { println!("Hello world"); }) }
So the code in async fn main()
is run by the executor, after its been started. If you want to run code before the executor has started, you can simply modify the generated code above or use the attribute on a different function:
#[tokio::main]
async fn start() {
println!("Hello world");
}
fn main() {
// do your thing
start();
}