FairMQ  1.4.33
C++ Message Queuing Library and Framework
Semaphore.h
1 /********************************************************************************
2  * Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_TOOLS_SEMAPHORE_H
10 #define FAIR_MQ_TOOLS_SEMAPHORE_H
11 
12 #include <condition_variable>
13 #include <cstdint>
14 #include <functional>
15 #include <memory>
16 #include <mutex>
17 
18 namespace fair::mq::tools
19 {
20 
25 struct Semaphore
26 {
27  Semaphore();
28  explicit Semaphore(std::size_t initial_count);
29 
30  auto Wait() -> void;
31  auto Signal() -> void;
32  auto GetCount() const -> std::size_t;
33 
34 private:
35  std::size_t fCount;
36  mutable std::mutex fMutex;
37  std::condition_variable fCv;
38 };
39 
44 struct SharedSemaphore
45 {
47  explicit SharedSemaphore(std::size_t initial_count);
48 
49  auto Wait() -> void;
50  auto Signal() -> void;
51  auto GetCount() const -> std::size_t;
52 
53 private:
54  std::shared_ptr<Semaphore> fSemaphore;
55 };
56 
57 } // namespace fair::mq::tools
58 
59 #endif /* FAIR_MQ_TOOLS_SEMAPHORE_H */
fair::mq::tools::SharedSemaphore
A simple copyable blocking semaphore.
Definition: Semaphore.h:51
fair::mq::tools::Semaphore
A simple blocking semaphore.
Definition: Semaphore.h:32

privacy