std.concurrent
Concurrency helper module.
yaoxiang
use std.concurrentThis module depends on operating-system threads and is not exported on the
wasm32target.
Functions Overview
| Function | Signature |
|---|---|
sleep | (millis: Int) -> Void |
thread_id | () -> String |
yield_now | () -> Void |
Functions
sleep
yaoxiang
sleep: (millis: Int) -> VoidBlocks the current thread for the specified number of milliseconds.
millis— Number of milliseconds to sleep; if not anIntor missing, it is treated as0(no error)
Unit note:
std.time.sleeptakes seconds and accepts decimals, while this function takes milliseconds.concurrent.sleep(1)sleeps for 1 millisecond,time.sleep(1)sleeps for 1 second.
yaoxiang
use std.concurrent
main: () -> Void = {
concurrent.sleep(0)
concurrent.sleep(1)
}thread_id
yaoxiang
thread_id: () -> StringReturns the identifier string of the current thread.
Returns: a string of the form ThreadId(1). The specific value varies with platform and scheduling, and should only be used for existence checks; do not depend on its specific content or format.
yaoxiang
use std.assert
use std.concurrent
use std.string
main: () -> Void = {
tid = concurrent.thread_id()
assert(string.len(tid) > 0)
}yield_now
yaoxiang
yield_now: () -> VoidVoluntarily yields the current thread's scheduling time slice, giving other threads a chance to run.
yaoxiang
use std.concurrent
main: () -> Void = {
concurrent.yield_now()
}Related
std.time.sleep— second-level sleep- Language Specification: Concurrency Model —
spawnand spawn semantics
