Libthreadar 1.6.0
barrier.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// libthreadar - is a library providing several C++ classes to work with threads
3// Copyright (C) 2014-2025 Denis Corbin
4//
5// This file is part of libthreadar
6//
7// libthreadar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// libhtreadar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with libthreadar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef LIBTHREADAR_BARRIER_HPP
25#define LIBTHREADAR_BARRIER_HPP
26
29
30#include "config.h"
31
32 // C system headers
33extern "C"
34{
35#if HAVE_PTHREAD_H
36#include <pthread.h>
37#endif
38}
39 // C++ standard headers
40
41
42 // libthreadar headers
43#include "condition.hpp"
44
45namespace libthreadar
46{
48
56 class barrier
57 {
58 public:
60
62 barrier(unsigned int num);
63
65 barrier(const barrier & ref) = delete;
66
68 barrier(barrier && ref) noexcept = default;
69
71 barrier & operator = (const barrier & ref) = delete;
72
74 barrier & operator = (barrier && ref) noexcept = default;
75
77
79 ~barrier() noexcept(false);
80
82
85 void wait();
86
88 unsigned int get_count() const { return val; };
89
91
95 unsigned int get_waiting_count() const { return waiting_num; };
96
97 static std::string used_implementation()
98 {
99#if HAVE_PTHREAD_BARRIER_T
100 return "pthread_barrier_t";
101#else
102 return "pthread_cond_t";
103#endif
104 }
105
106 private:
107 unsigned int val;
108 unsigned int waiting_num;
109
110#if HAVE_PTHREAD_BARRIER_T
111 pthread_barrier_t bar;
112#else
113 condition cond;
114#endif
115 };
116
118
119} // end of namespace
120
121#endif
the class barrier allows several threads to synchronize between them
Definition: barrier.hpp:57
~barrier() noexcept(false)
The destructor.
void wait()
suspend the calling thread waiting for other up to 'num' other thread to call wait too
barrier(barrier &&ref) noexcept=default
no move constructor
barrier(unsigned int num)
The constructor.
unsigned int get_count() const
return the barrier size
Definition: barrier.hpp:88
barrier & operator=(const barrier &ref)=delete
no assignment operator
unsigned int get_waiting_count() const
return the number of thread waiting on the barrier or just released from it
Definition: barrier.hpp:95
barrier(const barrier &ref)=delete
no copy constructor
defines the condition class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46