Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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 
13 #include <string>
14 
15 /*---------------------------------------------------------------
16  Class
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:
35  std::string m_name; //!< The name of the named semaphore, or empty if unnamed.
36 
37  public:
38  /** Creates a semaphore.
39  * If \a name is not an empty string, a named semaphore is created. In that case
40  * if the semaphore didn't exist it's created. Otherwise, the existing semaphore
41  * is linked to this object and then \a initialCount and \a maxCount are ignored.
42  * \note Named semaphores require Linux kernel version>2.6.12
43  */
44  CSemaphore(
45  unsigned int initialCount,
46  unsigned int maxCount,
47  const std::string &name=std::string("") );
48 
49  /** Destructor
50  */
51  virtual ~CSemaphore();
52 
53  /** Blocks until the count of the semaphore to be non-zero.
54  * \param timeout_ms The timeout in milliseconds, or set to zero to wait indefinidely.
55  * \return true if the semaphore has been signaled, false on timeout or any other error.
56  */
57  bool waitForSignal( unsigned int timeout_ms = 0 );
58 
59  /** Increments the count of the semaphore by a given amount.
60  */
61  void release(unsigned int increaseCount = 1);
62 
63 
64  /** Get the name of the named semaphore or an empty string if it's unnamed */
65  inline std::string getName() const { return m_name; }
66 
67  /** Return true if this is a named semaphore */
68  inline bool isNamed() const { return !m_name.empty(); }
69 
70 
71  };
72 
73  } // End of namespace
74 
75 } // End of namespace
76 
77 #endif
Represents a memory block (via "void*") that can be shared between several objects through copy opera...
std::string getName() const
Get the name of the named semaphore or an empty string if it&#39;s unnamed.
Definition: CSemaphore.h:65
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::utils::CReferencedMemBlock m_data
Definition: CSemaphore.h:34
bool isNamed() const
Return true if this is a named semaphore.
Definition: CSemaphore.h:68
A semaphore for inter-thread synchronization.
Definition: CSemaphore.h:31
std::string m_name
The name of the named semaphore, or empty if unnamed.
Definition: CSemaphore.h:35



Page generated by Doxygen 1.8.13 for MRPT 1.4.0 SVN: at Sat Sep 2 18:44:07 UTC 2017