MCPcopy Create free account
hub / github.com/VCVRack/Rack / Barrier

Class Barrier

src/engine/Engine.cpp:35–66  ·  view source on GitHub ↗

Multiple-phase barrier based on C++ mutexes, as a reference. */

Source from the content-addressed store, hash-verified

33/** Multiple-phase barrier based on C++ mutexes, as a reference.
34*/
35struct Barrier {
36 size_t threads = 0;
37 size_t count = 0;
38 size_t phase = 0;
39
40 std::mutex mutex;
41 std::condition_variable cv;
42
43 void setThreads(size_t threads) {
44 this->threads = threads;
45 }
46
47 void wait() {
48 std::unique_lock<std::mutex> lock(mutex);
49 size_t currentPhase = phase;
50
51 // Check if we're the last thread.
52 if (++count >= threads) {
53 // Advance phase and reset count
54 count = 0;
55 phase++;
56 // Notify all other threads
57 cv.notify_all();
58 return;
59 }
60
61 // Unlock and wait on phase to change
62 cv.wait(lock, [&] {
63 return phase != currentPhase;
64 });
65 }
66};
67
68
69/** Multiple-phase barrier based on spin-locking.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected