Thread module

Thread_Group

structSource

A pool of worker threads that run one procedure over pieces of work. Set it up with init, queue work with add_work, start the workers and collect results with get_completed_work. shutdown stops the workers.

Example↗
Work :: struct { input: int; result: int; }

square :: (group: *Thread_Group, thread: *Thread, work: *void) -> Thread_Continue_Status {
    item := cast(*Work) work;
    item.result = item.input * item.input;
    return .CONTINUE;
}

group: Thread_Group;
group.logging = false;
init(*group, 2, square);
items: [4] Work;
for *item, i: items {
    item.input = i + 1;
    add_work(*group, item);
}
start(*group);

assert(wait_for_all(*group));
assert(get_completed_work(*group).count == items.count);
assert(shutdown(*group));
assert(items[3].result == 16);

Fields

data: *void
name: string
logging: := true
time_proc: () -> float64
worker_start: (group: *Thread_Group, thread: *Thread, worker_index: s32)
Set before start to run something on each worker thread, once, before its first piece of work. worker_index is the worker's place in the group, 0 up to the number of threads minus one. A good place to build state the worker keeps.
allocator: Allocator
worker_info: [] Worker_Info
worker_info_data_to_free: *void
next_worker_index: s32
outstanding: s64
Work added and not yet finished, and what wait_for_all sleeps on.
idle_mutex: Mutex
idle_changed: Condition_Variable
initted: := false
started: := false
should_exit: := false

See also