Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
semaphore.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/semaphore.hpp
3  *
4  * A simple semaphore implementation using C++11 synchronization methods.
5  *
6  * Copied and modified from STXXL https://github.com/stxxl/stxxl, which is
7  * distributed under the Boost Software License, Version 1.0.
8  *
9  * Part of tlx - http://panthema.net/tlx
10  *
11  * Copyright (C) 2002 Roman Dementiev <[email protected]>
12  * Copyright (C) 2013-2018 Timo Bingmann <[email protected]>
13  *
14  * All rights reserved. Published under the Boost Software License, Version 1.0
15  ******************************************************************************/
16 
17 #ifndef TLX_SEMAPHORE_HEADER
18 #define TLX_SEMAPHORE_HEADER
19 
20 #include <condition_variable>
21 #include <mutex>
22 
23 namespace tlx {
24 
25 //! A simple semaphore implementation using C++11 synchronization methods.
26 class Semaphore
27 {
28 public:
29  //! construct semaphore
30  explicit Semaphore(size_t initial_value = 0)
31  : value_(initial_value) { }
32 
33  //! non-copyable: delete copy-constructor
34  Semaphore(const Semaphore&) = delete;
35  //! non-copyable: delete assignment operator
36  Semaphore& operator = (const Semaphore&) = delete;
37  //! move-constructor: just move the value
39  //! move-assignment: just move the value
40  Semaphore& operator = (Semaphore&& s) { value_ = s.value_; return *this; }
41 
42  //! function increments the semaphore and signals any threads that are
43  //! blocked waiting a change in the semaphore
44  size_t signal() {
45  std::unique_lock<std::mutex> lock(mutex_);
46  size_t res = ++value_;
47  cv_.notify_one();
48  return res;
49  }
50  //! function increments the semaphore and signals any threads that are
51  //! blocked waiting a change in the semaphore
52  size_t signal(size_t delta) {
53  std::unique_lock<std::mutex> lock(mutex_);
54  size_t res = (value_ += delta);
55  cv_.notify_all();
56  return res;
57  }
58  //! function decrements the semaphore and blocks if the semaphore is < delta
59  //! until another thread signals a change
60  size_t wait(size_t delta = 1) {
61  std::unique_lock<std::mutex> lock(mutex_);
62  while (value_ < delta)
63  cv_.wait(lock);
64  value_ -= delta;
65  return value_;
66  }
67 
68  //! return the current value -- should only be used for debugging.
69  size_t value() const { return value_; }
70 
71 private:
72  //! value of the semaphore
73  size_t value_;
74 
75  //! mutex for condition variable
76  std::mutex mutex_;
77 
78  //! condition variable
79  std::condition_variable cv_;
80 };
81 
82 //! alias for STL-like code style
84 
85 } // namespace tlx
86 
87 #endif // !TLX_SEMAPHORE_HEADER
88 
89 /******************************************************************************/
size_t value() const
return the current value – should only be used for debugging.
Definition: semaphore.hpp:69
A simple semaphore implementation using C++11 synchronization methods.
Definition: semaphore.hpp:26
size_t value_
value of the semaphore
Definition: semaphore.hpp:73
static constexpr double delta
Definition: select.hpp:35
std::condition_variable cv_
condition variable
Definition: semaphore.hpp:79
size_t wait(size_t delta=1)
Definition: semaphore.hpp:60
Semaphore(Semaphore &&s)
move-constructor: just move the value
Definition: semaphore.hpp:38
size_t signal()
Definition: semaphore.hpp:44
std::mutex mutex_
mutex for condition variable
Definition: semaphore.hpp:76
Semaphore & operator=(const Semaphore &)=delete
non-copyable: delete assignment operator
Semaphore(size_t initial_value=0)
construct semaphore
Definition: semaphore.hpp:30
size_t signal(size_t delta)
Definition: semaphore.hpp:52