8000 GitHub - swnb/coroutine-scheduler
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

swnb/coroutine-scheduler

Repository files navigation

单线程协程

示例

let runtime = Runtime::new();

// 创建多个协程任务,每个任务会执行多次并主动让出控制权
for task_id in 1..=5 {
    runtime.spawn({
        let runtime = runtime.clone();
        move || {
            for step in 1..=3 {
                println!("任务 {} 正在执行步骤 {}", task_id, step);
                if step < 3 {
                    // 主动让出控制权,让其他协程有机会执行
                    runtime.schedule();
                }
            }
            println!("任务 {} 完成", task_id);
        }
    });
}

println!("开始运行所有协程...");
runtime.wait();
println!("所有协程执行完成!");

result

仅用于学习目的

使用 arm64 汇编,没考虑其它平台

zhihu 详解文章

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0