6 ms·
You can have barriers that can schedule "something else" if the barrier is not ready, by using the services provided by the operating system. For instance, in
by adrian_b 3y ago
You can have barriers that can schedule "something else" if the barrier is not ready, by using the services provided by the operating system.
For instance, in Linux you can use the syscalls FUTEX_WAIT and FUTEX_WAKE.
Instead of waiting in a loop for the barrier variable to reach the right value, invoking FUTEX_WAIT will schedule "something else". Whoever modifies the value of the barrier variable invokes FUTEX_WAKE, so that everybody who waits will check if the new value is what they are waiting for, otherwise they will yield execution again.
In Windows there is the analogous function WaitOnAddress().
Without using the operating system, on Intel or AMD CPUs it is possible to use the instructions MONITOR and MWAIT, or their unprivileged variants UMONITOR and UMWAIT (the latter are available only in some recent models).
By invoking MWAIT/UMWAIT, a thread will sleep until another thread will execute a store instruction towards the monitored barrier variable, changing its value.