Lumiera
0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
sync-barrier.hpp
Go to the documentation of this file.
1
/*
2
SYNC-BARRIER.hpp - N-fold synchronisation point with yield-wait
3
4
Copyright (C)
5
2023, Hermann Vosseler <Ichthyostega@web.de>
6
7
**Lumiera** is free software; you can redistribute it and/or modify it
8
under the terms of the GNU General Public License as published by the
9
Free Software Foundation; either version 2 of the License, or (at your
10
option) any later version. See the file COPYING for further details.
11
12
*/
13
14
40
#ifndef LIB_SYNC_BARRIER_H
41
#define LIB_SYNC_BARRIER_H
42
43
44
#include "
lib/error.hpp
"
45
#include "
lib/nocopy.hpp
"
46
47
#include <thread>
48
#include <atomic>
49
50
51
namespace
lib
{
52
53
64
class
SyncBarrier
65
:
util::NonCopyable
66
{
67
std::atomic_int
latch_
;
68
69
public
:
71
explicit
72
SyncBarrier
(
uint
nFold =2)
73
:
latch_
{int(nFold)}
74
{
75
REQUIRE (nFold >= 2,
"Pointless to sync less than two participants."
);
76
ENSURE (nFold < 100'000,
"Danger territory.... sync 100k Threads??"
);
77
}
78
79
void
80
sync
()
81
{
82
size_t
level =
latch_
.fetch_add(-1, std::memory_order_acq_rel);
83
if
(1 < level)
84
do
std::this_thread::yield();
85
while
(0 <
latch_
.load (std::memory_order_relaxed));
86
else
87
latch_
.store (0, std::memory_order_relaxed);
88
}
// prevent spurious calls from wrapping
89
};
90
91
92
93
}
// namespace lib
94
#endif
/*LIB_SYNC_BARRIER_H*/
lib::SyncBarrier
A one time N-fold mutual synchronisation barrier.
Definition
sync-barrier.hpp:66
lib::SyncBarrier::SyncBarrier
SyncBarrier(uint nFold=2)
Definition
sync-barrier.hpp:72
lib::SyncBarrier::latch_
std::atomic_int latch_
Definition
sync-barrier.hpp:67
lib::SyncBarrier::sync
void sync()
Definition
sync-barrier.hpp:80
util::NonCopyable
Any copy and copy construction prohibited.
Definition
nocopy.hpp:38
error.hpp
Lumiera error handling (C++ interface).
uint
unsigned int uint
Definition
integral.hpp:29
lib
Implementation namespace for support and library code.
Definition
common-services.cpp:55
nocopy.hpp
Mix-Ins to allow or prohibit various degrees of copying and cloning.
lib
sync-barrier.hpp
Generated on Fri Nov 7 2025 for Lumiera by
1.9.8