Thread module

wait_for_all

procedureSource

Waits until every piece of work added to group has been run by a worker, or until timeout_milliseconds pass (-1 waits as long as it takes). Returns false on a timeout. The group has to be started, and has to have a live worker to run the work: if every worker stopped with work still queued, only a timeout ends the wait.

What was run is then ready for get_completed_work. More work can be added afterwards.

Example↗
Counter :: struct { mutex: Mutex; total: s64; }

count :: (group: *Thread_Group, thread: *Thread, work: *void) -> Thread_Continue_Status {
    counter := cast(*Counter) group.data;
    lock(*counter.mutex);
    counter.total += 1;
    unlock(*counter.mutex);
    return .CONTINUE;
}

counter: Counter;
init(*counter.mutex);
group: Thread_Group;
group.logging = false;
init(*group, 4, count);
group.data = *counter;
start(*group);
for 1..100 add_work(*group, null);

assert(wait_for_all(*group));
assert(counter.total == 100);
assert(shutdown(*group));
destroy(*counter.mutex);
wait_for_all :: (group: *Thread_Group, timeout_milliseconds: s32 = -1) -> bool

Waits until every piece of work added to group has been run by a worker, or until timeout_milliseconds pass (-1 waits as long as it takes). Returns false on a timeout. The group has to be started, and has to have a live worker to run the work: if every worker stopped with work still queued, only a timeout ends the wait.

What was run is then ready for get_completed_work. More work can be added afterwards.

Parameters

group: *Thread_Group
timeout_milliseconds: s32 = -1
how long to wait at most, or -1 for no limit.

Returns

bool
Example↗
Counter :: struct { mutex: Mutex; total: s64; }

count :: (group: *Thread_Group, thread: *Thread, work: *void) -> Thread_Continue_Status {
    counter := cast(*Counter) group.data;
    lock(*counter.mutex);
    counter.total += 1;
    unlock(*counter.mutex);
    return .CONTINUE;
}

counter: Counter;
init(*counter.mutex);
group: Thread_Group;
group.logging = false;
init(*group, 4, count);
group.data = *counter;
start(*group);
for 1..100 add_work(*group, null);

assert(wait_for_all(*group));
assert(counter.total == 100);
assert(shutdown(*group));
destroy(*counter.mutex);