Main MRPT website > C++ reference for MRPT 1.5.9
CSemaphore.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef mrpt_synch_semaphore_H
10 #define mrpt_synch_semaphore_H
11 
12 #include <string>
13 #include <mutex>
14 #include <condition_variable>
15 #include <exception>
16 #include <mrpt/base/link_pragmas.h>
17 
18 namespace mrpt
19 {
20  namespace synch
21  {
22  /** A semaphore for inter-thread synchronization.
23  * The state of a semaphore object is signaled when its count is greater than zero,
24  * and nonsignaled when its count is equal to zero. The initialCount parameter specifies
25  * the initial count. Each time a waiting thread is released because of the semaphore's
26  * signaled state, the count of the semaphore is decreased by one. Use the release function
27  * to increment a semaphore's count by a specified amount. The count can never be less
28  * than zero or greater than the value specified in the maxCount parameter.
29  * \ingroup synch_grp
30  */
32  {
33  protected:
34  std::mutex m_mutex;
35  std::condition_variable m_condition;
36  unsigned long m_count{ 0 };
37 
38  public:
39  /** Creates a semaphore.*/
40  CSemaphore(unsigned int initialCount, unsigned int maxCount);
41 
42  // throw instead of deleting the copy operator to allow mrpt-1.5 legacy code to build.
43  CSemaphore(const CSemaphore&o) { throw std::runtime_error("CSemaphore cannot be copied"); }
44 
45  /** Blocks until the count of the semaphore to be non-zero.
46  * \param timeout_ms The timeout in milliseconds, or set to zero to wait indefinidely.
47  * \return true if the semaphore has been signaled, false on timeout or any other error.
48  */
49  bool waitForSignal( unsigned int timeout_ms = 0 );
50 
51  /** Increments the count of the semaphore by a given amount */
52  void release(unsigned int increaseCount = 1);
53  };
54 
55  } // End of namespace
56 } // End of namespace
57 #endif
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CSemaphore(const CSemaphore &o)
Definition: CSemaphore.h:43
GLsizei maxCount
Definition: glext.h:3902
std::condition_variable m_condition
Definition: CSemaphore.h:35
A semaphore for inter-thread synchronization.
Definition: CSemaphore.h:31



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020