tokio 什么时候生成线程?

问题描述

如果我的代码有

#[tokio::main]
async fn main() {
  // mutates a global read-only variable "unsafe"

全局只读变量的变化会在 Tokio 设置其线程池之前还是之后发生?

解决方法

来自tokio documentation

#[tokio::main]
async fn main() {
    println!("Hello world");
}

不使用 #[tokio::main] 的等效代码

fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            println!("Hello world");
        })
}

所以 async fn main() 中的代码在执行器启动后由执行器运行。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...