Libthreadar  1.4.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-2020 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
34 extern "C"
35 {
36 
37 }
38  // C++ standard headers
39 #include <string>
40 
41 
42  // libthreadar headers
43 #include "mutex.hpp"
44 
45 namespace libthreadar
46 {
47 
49 
52  class semaphore
53  {
54  public:
56 
63  semaphore(unsigned int max_value);
64 
66  semaphore(const semaphore & ref) = delete;
67 
69  semaphore(semaphore && ref) noexcept = default;
70 
72  semaphore & operator = (const semaphore & ref) = delete;
73 
75  semaphore & operator = (semaphore && ref) noexcept = default;
76 
78  ~semaphore();
79 
81  bool waiting_thread() const;
82 
83 
85  bool working_thread() const;
86 
88 
94  void lock();
95 
97 
100  void unlock();
101 
103  void reset();
104 
106 
109  int get_value() const { return value; };
110 
111  private:
112  int value; //< this is the semaphore value
113  mutex val_mutex; //< this controls modification to value
114  mutex semaph; //< this mutex is used to suspend thread semaphore value get negative
115  int max_value; //< maximum value the semaphore cannot exceed
116  };
117 
118 } // end of namespace
119 
120 #endif
Class semaphore is an enhanced version of Posix semaphore.
Definition: semaphore.hpp:52
semaphore & operator=(const semaphore &ref)=delete
no assignment operator
defines the mutex C++ class
void lock()
Request a "resource".
bool waiting_thread() const
Return whether the semaphore has at least a pending thread waiting for another thread to unlock it...
bool working_thread() const
return whether the semaphore has at least one thread that acquired the lock, possibily without other ...
void unlock()
Release a "resource".
void reset()
Reset to initial state releasing any thread that could wait on the semaphore.
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:45
semaphore(unsigned int max_value)
semaphore constuctor
Wrapper around the Posix pthread_mutex_t C objects.
Definition: mutex.hpp:56
~semaphore()
Destructor.
int get_value() const
Return the value of the semaphore, that's to say the number of available "resources".
Definition: semaphore.hpp:109