Libthreadar  1.0.1
 All Classes Namespaces Files Functions Macros
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-2015 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 
65  // no copy constructor (made private)
66 
67  // no assignment operator (made private)
68 
70  ~semaphore();
71 
73  bool waiting_thread() const;
74 
75 
77  bool working_thread() const;
78 
80 
86  void lock();
87 
89 
92  void unlock();
93 
95  void reset();
96 
98 
101  int get_value() const { return value; };
102 
103  private:
105  semaphore(const semaphore & ref):max_value(0) { throw std::string("BUG"); };
106 
108  const semaphore & operator = (const semaphore & ref) { throw std::string("BUG"); };
109 
110  int value; //< this is the semaphore value
111  mutex val_mutex; //< this controls modification to value
112  mutex semaph; //< this mutex is used to suspend thread semaphore value get negative
113  const int max_value; //< maximum value the semaphore cannot exceed
114  };
115 
116 } // end of namespace
117 
118 #endif
Class semaphore is an enhanced version of Posix semaphore.
Definition: semaphore.hpp:52
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:46
semaphore(unsigned int max_value)
semaphore constuctor
~semaphore()
Destructor.
int get_value() const
Return the value of the semaphore, that's to say the number of available "resources".
Definition: semaphore.hpp:101