Lumiera  0.pre.03
»edit your freedom«
sync-barrier.hpp File Reference

Go to the source code of this file.

Description

A N-fold synchronisation latch using yield-wait until fulfilment.

Semantically, this barrier is similar to std::latch (introduced with C++20). It is intended for one-time use and blocks any invocation until N invocations occurred. The »wait« however is implemented as a spinning loop with std::this_thread::yield, which places this implementation into a middle ground between a full-fledged blocking lock and a busy spinning log. The yield() call is expected to de-prioritise the current thread behind all other threads currently amenable for active execution, without actually placing the thread into sleeping state. The coordination of the latch relies on Atomics.

See also
SyncBarrier_test
steam::control::DispatcherLoop
Todo:
as of 9/2023 it remains to be seen if this facility is just a pre-C++20 workaround; otherwise it may present distinct performance characteristics than std::latch, possibly also a slightly more abstracted (and thus clearer) usage API.
Remarks
Typical overhead measured with optimised build on 8 Core machine
  • Sync 2 threads : 280ns
  • Sync 4 threads : 700ns
  • increasing with number of threads, which implies we are measuring the time it takes all threads to catch-up on average...
  • the classical Mutex+Condition-Var solution is slower by orders of magnitude!
  • these values are on par with typical thread scheduling leeway, so this implementation seems adequate for the time being (2023).

Definition in file sync-barrier.hpp.

#include "lib/error.hpp"
#include "lib/nocopy.hpp"
#include <thread>
#include <atomic>

Classes

class  SyncBarrier
 A one time N-fold mutual synchronisation barrier. More...
 

Namespaces

 lib
 Implementation namespace for support and library code.