LYR-SEM0213
a module 'var' in a program that starts threads
A module of a program that imports std.thread declares a module-level var.
Explanation
Tasks of one thread take turns, but threads run at once: two of them may read and write a module's var at the same time, and a value half written is read.
Hold the value in an Atomic, or behind a Mutex (std.sync), or keep it local to one thread. A var that a lock of its own guards says so with @Allow("global-var-without-sync").
Wrong
import std.thread { Thread };
var hits = 0;
fn main(): void {
hits = hits + 1;
let _ = Thread.spawn(() => 1);
}
Right
import std.thread { Thread };
fn main(): void {
var hits = 0;
hits = hits + 1;
let _ = hits;
let _ = Thread.spawn(() => 1);
}