Libthreadar 1.6.0
semaphore.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_SEMAPHORE_HPP
25#define LIBTHREADAR_SEMAPHORE_HPP
26
30
31#include "config.h"
32
33 // C system headers
34extern "C"
35{
36
37}
38 // C++ standard headers
39#include <string>
40
41
42 // libthreadar headers
43#include "mutex.hpp"
44
45namespace libthreadar
46{
47
49
53 {
54 public:
56
65 semaphore(unsigned int max_value);
66
68 semaphore(const semaphore & ref) = delete;
69
71 semaphore(semaphore && ref) noexcept = default;
72
74 semaphore & operator = (const semaphore & ref) = delete;
75
77 semaphore & operator = (semaphore && ref) noexcept = default;
78
81
83 bool waiting_thread() const;
84
85
87 bool working_thread() const;
88
90
96 void lock();
97
99
102 void unlock();
103
105 void reset();
106
108
111 int get_value() const { return value; };
112
113 private:
114 int value; //< this is the semaphore value
115 mutex val_mutex; //< this controls modification to value
116 mutex semaph; //< this mutex is used to suspend thread semaphore value get negative
117 int max_value; //< maximum value the semaphore cannot exceed
118 };
119
120} // end of namespace
121
122#endif
Wrapper around the Posix pthread_mutex_t C objects.
Definition: mutex.hpp:57
Class semaphore is an enhanced version of Posix semaphore.
Definition: semaphore.hpp:53
semaphore(const semaphore &ref)=delete
no copy constructor
bool working_thread() const
return whether the semaphore has at least one thread that acquired the lock, possibily without other ...
semaphore(unsigned int max_value)
semaphore constuctor
~semaphore()
Destructor.
semaphore(semaphore &&ref) noexcept=default
no move constructor
void lock()
Request a "resource".
int get_value() const
Return the value of the semaphore, that's to say the number of available "resources".
Definition: semaphore.hpp:111
bool waiting_thread() const
Return whether the semaphore has at least a pending thread waiting for another thread to unlock it.
void reset()
Reset to initial state releasing any thread that could wait on the semaphore.
semaphore & operator=(const semaphore &ref)=delete
no assignment operator
void unlock()
Release a "resource".
defines the mutex C++ class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46