std.sync¶
Goroutine-style concurrency primitives: channels for passing values between
green threads, plus a mutex, a wait group, timed sleeping, and a multi-channel
receive. std.sync pairs with the spawn { ... } statement, which starts a
goroutine. Goroutines run on an M:N scheduler that
spreads them across a pool of worker threads (one per available core), so they
run in parallel. Within a worker, scheduling is cooperative: a goroutine keeps
running until it hits a yield point (a blocking channel operation or an explicit
yield_now()), at which the worker resumes another ready goroutine.
from std.sync import channel, channel_buffered, yield_now
fun main() {
let ch = channel()
spawn {
ch.send(42)
}
print(ch.recv()) // 42
}
Channels in this slice carry int values.
Importing¶
channel, channel_buffered, and yield_now are free functions, so import
the ones you use by name. send and recv are methods on Channel and need
no separate import once you hold a channel value.
The spawn keyword is part of the language itself and needs no import. spawn { ... }
runs the block as a goroutine and returns a handle whose join() waits for it.
Creating channels¶
channel() -> Channel¶
Create an unbuffered (rendezvous) channel. A send blocks until a receiver is
ready to take the value, and a recv blocks until a sender hands one over. The
two sides meet: nothing is stored in between.
from std.sync import channel, channel_buffered, yield_now
fun main() {
let ch = channel()
spawn {
ch.send(7) // blocks until main receives
}
print(ch.recv()) // 7
}
channel_buffered(cap: int) -> Channel¶
Create a buffered channel of capacity cap. A send returns immediately while
there is room in the buffer, and only blocks once the buffer is full. A recv
takes the oldest buffered value, and blocks only when the buffer is empty.
from std.sync import channel, channel_buffered, yield_now
fun main() {
let ch = channel_buffered(2)
ch.send(1) // room: returns at once
ch.send(2) // room: returns at once
print(ch.recv()) // 1
print(ch.recv()) // 2
}
Channel methods¶
send(self, value: int)¶
Send value, blocking until the channel can accept it. On an unbuffered
channel it blocks until a receiver is ready; on a buffered channel it blocks
only when the buffer is full. When a send blocks, the goroutine yields to the
scheduler so other goroutines can run.
recv(self) -> int¶
Receive a value, blocking until one is available. On an empty channel the goroutine yields to the scheduler and resumes when a sender delivers a value.
from std.sync import channel, channel_buffered, yield_now
fun main() {
let ch = channel()
spawn {
ch.send(10)
ch.send(20)
}
print(ch.recv()) // 10
print(ch.recv()) // 20
}
Yielding¶
yield_now()¶
Yield control to the scheduler so other ready goroutines can run, then resume
later. This is the explicit cooperative yield point. You rarely need it when
your goroutines communicate over channels, since send and recv already
yield when they block, but it is useful for handing off in a tight loop that
otherwise never reaches a blocking operation.
from std.sync import channel, channel_buffered, yield_now
fun main() {
spawn {
print(1)
yield_now()
print(3)
}
print(2)
}
Pairing channels with spawn¶
spawn { ... } starts a goroutine. The block can capture channels from the surrounding scope and use them to communicate with
main (which is itself goroutine 0) or with other goroutines.
from std.sync import channel, channel_buffered, yield_now
fun main() {
let ch = channel()
// Producer goroutine: send three values, then a sentinel.
spawn {
ch.send(100)
ch.send(200)
ch.send(300)
ch.send(-1)
}
// Main consumes until the sentinel.
loop {
let v = ch.recv()
if v == -1 {
break
}
print(v) // 100, 200, 300
}
}
When main returns the program exits, and any goroutines still alive (ready or
blocked) are abandoned without finishing. If every goroutine ends up blocked
with none ready, the scheduler reports a deadlock and exits.
Worked example: a worker over a buffered channel¶
A buffered channel decouples the producer from the consumer so the producer can get ahead while the consumer catches up.
from std.sync import channel_buffered, yield_now
fun main() {
let jobs = channel_buffered(4)
// Worker: read each job and print its square.
spawn {
loop {
let n = jobs.recv()
if n == 0 {
break
}
print(n * n) // 1, 4, 9, 16
}
}
// Feed work, then a 0 to signal done.
var k = 1
while k <= 4 {
jobs.send(k)
k = k + 1
}
jobs.send(0)
// Give the worker a turn to drain the channel before main exits.
yield_now()
}
Releasing a channel¶
free(self)¶
Release a channel's runtime resources when you are done with it. A channel is
otherwise reclaimed when the program exits; free is for a long-running program
that creates many channels over time.
Sleeping¶
sleep_millis(ms: int)¶
Block the current goroutine for ms milliseconds. The goroutine parks on a
timer, so its worker thread runs other goroutines in the meantime.
from std.sync import sleep_millis
fun main() {
spawn {
sleep_millis(50)
print(2)
}
print(1)
sleep_millis(100) // give the goroutine time to finish
}
Mutex¶
A mutex guards a critical section so only one goroutine holds it at a time, which matters now that goroutines run in parallel across worker threads.
Mutex.new(), lock(self), unlock(self)¶
mutex() is the same constructor as a free function.
from std.sync import Mutex
fun main() {
let m = Mutex.new()
m.lock()
// ... exclusive section ...
m.unlock()
}
Wait group¶
A wait group counts outstanding work and lets one goroutine block until it reaches zero, the usual way to wait for spawned goroutines to finish.
WaitGroup.new()¶
Create a wait group with a zero counter. wait_group() does the same.
add(self, n: int), done(self), wait(self), free(self)¶
add(n) raises the counter by n (call it before spawning the work),
done() lowers it by one (call it as each unit finishes), wait() blocks
until the counter reaches zero, and free() releases the wait group.
from std.sync import wait_group, sleep_millis
fun main() {
let wg = wait_group()
wg.add(2)
spawn {
sleep_millis(20)
wg.done()
}
spawn {
sleep_millis(20)
wg.done()
}
wg.wait() // both goroutines have finished
wg.free()
}
Selecting over several channels¶
select_recv(channels: list[Channel]) -> SelectResult¶
Block until one of channels has a value to receive, then return a
SelectResult with index (the position in the list of the channel that
produced the value, or -1 when the list is empty) and value (that value).
When several channels are ready, the lowest index wins.
from std.sync import channel_buffered, select_recv
fun main() {
let a = channel_buffered(1)
let b = channel_buffered(1)
b.send(22)
let chosen = select_recv([a, b])
print(chosen.index) // 1
print(chosen.value) // 22
}
Timeouts¶
ch.recv_timeout(ms) returns Some(value), or None if nothing arrives in
time. ch.try_recv() takes a value only if one is already waiting.
select_recv_timeout(channels, ms) is select_recv with the same limit.
from std.sync import channel_buffered
fun main() {
let jobs: Channel[str] = channel_buffered(4)
match jobs.recv_timeout(10) {
case Some(job) { print(job) }
case None { print("no job yet") }
}
}
Timers and tickers¶
after(ms) is a one-shot Timer: wait() blocks until it fires, and
stop() cancels it (returning false if it already fired). ticker(ms)
fires every ms milliseconds; tick() waits for the next one and stop()
ends it. A tick nobody waits for is dropped, so a slow loop does not fall
behind. Waiting on either parks the goroutine, not its thread.
from std.sync import after, ticker
fun main() {
after(10).wait()
let t = ticker(5)
for _ in range(3) {
t.tick()
}
t.stop()
print("done")
}
Atomic integers¶
AtomicInt.new(v) is an integer goroutines update without a lock: load,
store, add(delta) (returns the new value), swap(v) (returns the old
one), and compare_and_swap(expected, new). Call free() when it is no
longer used.
Read-write lock, once, and semaphore¶
RwLock.new(): any number of goroutines holdread_lock()at once, or one holdswrite_lock(). A waiting writer blocks new readers so it is not starved. Pair each withread_unlock()orwrite_unlock().Once.new():call(f)runsfthe first time only, and other callers wait until it has finished.is_done()reports whether it ran.Semaphore.new(n): at mostngoroutines hold a permit betweenacquire()andrelease().
from std.sync import Semaphore, WaitGroup, sleep_millis
fun fetch(sem: Semaphore, wg: WaitGroup) {
sem.acquire()
sleep_millis(5) // at most 2 of these run at once
sem.release()
wg.done()
}
fun main() {
let sem = Semaphore.new(2)
let wg = WaitGroup.new()
wg.add(6)
for _ in range(6) {
spawn fetch(sem, wg)
}
wg.wait()
print("all fetched")
}
Deadlocks¶
When every goroutine is blocked on a channel and nothing can wake them, the
program stops with all goroutines are blocked: deadlock. A pending timer or
an open std.signal subscription counts as something that can wake them.
See also¶
- The language reference for
spawn, goroutines, the parallel scheduler, and deadlock behavior.