/
barrier.hpp
59 lines (50 loc) · 1.96 KB
/
barrier.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
// Copyright (c) 2007-2013 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(HPX_LCOS_BARRIER_JUN_23_2008_0530PM)
#define HPX_LCOS_BARRIER_JUN_23_2008_0530PM
#include <hpx/lcos/local/detail/condition_variable.hpp>
#include <hpx/lcos/local/spinlock.hpp>
#include <boost/thread/lock_types.hpp>
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace lcos { namespace local
{
/// A barrier can be used to synchronize a specific number of threads,
/// blocking all of the entering threads until all of the threads have
/// entered the barrier.
///
/// \note A \a barrier is not a LCO in the sense that it has no global id
/// and it can't be triggered using the action (parcel) mechanism.
/// It is just a low level synchronization primitive allowing to
/// synchronize a given number of \a threads.
class barrier
{
private:
typedef lcos::local::spinlock mutex_type;
public:
barrier(std::size_t number_of_threads)
: number_of_threads_(number_of_threads)
{}
/// The function \a wait will block the number of entering \a threads
/// (as given by the constructor parameter \a number_of_threads),
/// releasing all waiting threads as soon as the last \a thread
/// entered this function.
void wait()
{
boost::unique_lock<mutex_type> l(mtx_);
if (cond_.size(l) < number_of_threads_-1) {
cond_.wait(l, "barrier::wait");
}
else {
// release the threads
cond_.notify_all(std::move(l));
}
}
private:
std::size_t const number_of_threads_;
mutable mutex_type mtx_;
local::detail::condition_variable cond_;
};
}}}
#endif