Main MRPT website > C++ reference
MRPT logo
CSemaphore.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_synch_semaphore_H
29 #define mrpt_synch_semaphore_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/utils/CStream.h>
34 
35 /*---------------------------------------------------------------
36  Class
37  ---------------------------------------------------------------*/
38 namespace mrpt
39 {
40  namespace synch
41  {
42  /** A semaphore for inter-thread synchronization.
43  * The state of a semaphore object is signaled when its count is greater than zero,
44  * and nonsignaled when its count is equal to zero. The initialCount parameter specifies
45  * the initial count. Each time a waiting thread is released because of the semaphore's
46  * signaled state, the count of the semaphore is decreased by one. Use the release function
47  * to increment a semaphore's count by a specified amount. The count can never be less
48  * than zero or greater than the value specified in the maxCount parameter.
49  * \ingroup synch_grp
50  */
52  {
53  protected:
55  std::string m_name; //!< The name of the named semaphore, or empty if unnamed.
56 
57  public:
58  /** Creates a semaphore.
59  * If \a name is not an empty string, a named semaphore is created. In that case
60  * if the semaphore didn't exist it's created. Otherwise, the existing semaphore
61  * is linked to this object and then \a initialCount and \a maxCount are ignored.
62  */
63  CSemaphore(
64  unsigned int initialCount,
65  unsigned int maxCount,
66  const std::string &name=std::string("") );
67 
68  /** Destructor
69  */
70  virtual ~CSemaphore();
71 
72  /** Blocks until the count of the semaphore to be non-zero.
73  * \param timeout_ms The timeout in milliseconds, or set to zero to wait indefinidely.
74  * \return true if the semaphore has been signaled, false on timeout or any other error.
75  */
76  bool waitForSignal( unsigned int timeout_ms = 0 );
77 
78  /** Increments the count of the semaphore by a given amount.
79  */
80  void release(unsigned int increaseCount = 1);
81 
82 
83  /** Get the name of the named semaphore or an empty string if it's unnamed */
84  inline std::string getName() const { return m_name; }
85 
86  /** Return true if this is a named semaphore */
87  inline bool isNamed() const { return !m_name.empty(); }
88 
89 
90  };
91 
92  } // End of namespace
93 
94 } // End of namespace
95 
96 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013