-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcyclicbarrier.hpp
66 lines (55 loc) · 1.78 KB
/
cyclicbarrier.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef __CYCLICBARRIER_NIPUN__
#define __CYCLICBARRIER_NIPUN__
#include <inttypes.h>
#include <stdint.h>
#include <mutex>
#include <condition_variable>
namespace cbar
{
class callable
{
public:
virtual void run() = 0;
};
class cyclicbarrier {
public:
/*! Constructor
\param parties how many callers has to wait before all of them are waked up
\param call , callable object which is fired once waiters are signaled
*/
cyclicbarrier(uint32_t parties, callable* call=0);
/*!
reset makes the cyclicbarrier object reusable, also wakes up any threads
waiting on it
*/
void reset();
/*!
await is called to await for a number for required number of parties to wait
upon the barrier object.
\param nanosecs is waittime in nanoseconds, by default it is zero which specifies
indefinite wait
*/
void await(uint64_t nanosecs=0);
/*!
get_barrier_size returns the number of parties required to wait upon the object, before
the waiters are woke up
*/
uint32_t get_barrier_size() const;
/*!
get_current_waiting returns how many threads are currently waiting on the object
*/
uint32_t get_current_waiting() const;
private:
std::condition_variable cv;
std::mutex lock;
std::mutex reset_lock;
uint32_t parties;
uint32_t current_waits;
callable *call;
// deleted constructors/assignmenet operators
cyclicbarrier() = delete;
cyclicbarrier(const cyclicbarrier& other) = delete;
cyclicbarrier& operator=(const cyclicbarrier& opther) = delete;
};
}
#endif