Q+ 文档

并发总览(P2-C5 → 1.7.0-dev / v4.38)

English summary. Concurrency reference: async stackful tasks, OS threads, bounded channels and thread pools — all VM-only (AOT traps loudly). Cooperative scheduling; results are i32; copied values across threads; default 16 tasks × 256 KB, 1000-timer build is test-only; cancellation is cooperative; async IO A-lite is VM-only.

Q+ 1.6.0-dev 的并发能力分两条线:单线程 stackful async 任务(VM 绿色线程 + event-loop) 与 OS 线程/channel/线程池(pthread 后端)。两者都是消息传递模型:线程之间不共享可变对象, 只通过 channel 传值。

重要边界:以上并发原语(含 1.4.x 的 async IO A-lite)目前都是 VM-only。AOT ELF 可以构建 import 了 std.async / std.thread 的程序,但执行到并发原语时 loud trap (AOT_UNSUPPORTED_*,不静默、不产生错误结果)。async-IO 程序在 AOT 构建期即 loud fail。

async 任务(use std.async;)

API语义
spawn(fn() -> i32) -> i32创建 stackful 绿色任务,返回 task id
yield_now()让出执行权(协作式调度)
task_wait(id) / await_task(id) / await <id>等待任务完成并取 i32 结果
task_state(id)0 新建 / 1 就绪 / 2 运行 / 3 挂起 / 4 完成 / 5 取消
task_sleep(ms)真实挂起,由执行器按 deadline 唤醒(非忙等)
task_wait_timeout(id, ms)超时返回 -2(任务继续,不杀);取消返回 -3
task_cancel(id)协作式取消,沿 creator-tree / waiter-chain 传播
use std.async;
use std.io;

fn worker() -> i32 {
    println_str("task start")
    task_sleep(10)
    println_str("task done")
    return 11
}

fn main() -> i32 {
    let t: i32 = spawn(worker)
    let r: i32 = task_wait(t)
    println_int(r)
    return 0
}

async IO A-lite(1.4.x,VM-only)

std.async 在 stackful 任务上提供 Linux epoll + timer + waker 的 event-loop:

容量与测试配置(重要,不得当生产默认):

线程与 channel(use std.thread;)

API语义
thread_spawn(fn() -> i32) / thread_spawn_arg(fn(i32) -> i32, arg)新建 OS 线程(独立子 BQVM)
thread_join(id) / thread_join_timeout(id, ms)join;超时 -2 后仍可再 join;重复 join 幂等
thread_last_error(id)0=ok / 1=runtime error / 2=panic(join 后保留)
channel_new(cap)、channel_send/recv、*_i64、*_str有界消息队列;string 跨线程按字节复制(≤1 MiB),接收方堆分配
channel_close / channel_is_closed / *_timeout关闭后 send loud;已入队可 drain;超时 -2、关闭 -3
pool_new/submit/join/shutdown/job_result固定 worker 线程池(子 VM/作业)

线程错误/panic 以 [thread <tid>] Runtime error: ... + traceback 打到 stderr,主线程继续; 每线程/池作业独立 BQVM,因此无共享可变状态。P2-C10 安全修复(double-join loud、spawn 上限 loud、async 主线程专属、阻塞 channel 可被 --timeout-ms 中断、UAF-free teardown)保持生效。

不得宣称(本轮相关)

← Q+ Hub