Main MRPT website > C++ reference
MRPT logo
CSemaphore.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  mrpt_synch_semaphore_H
00029 #define  mrpt_synch_semaphore_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CStream.h>
00033 #include <mrpt/utils/CReferencedMemBlock.h>
00034 
00035 /*---------------------------------------------------------------
00036         Class
00037   ---------------------------------------------------------------*/
00038 namespace mrpt
00039 {
00040         namespace synch
00041         {
00042                 /** A semaphore for inter-thread synchronization.
00043                   * The state of a semaphore object is signaled when its count is greater than zero,
00044                   *  and nonsignaled when its count is equal to zero. The initialCount parameter specifies
00045                   *  the initial count. Each time a waiting thread is released because of the semaphore's
00046                   *  signaled state, the count of the semaphore is decreased by one. Use the release function
00047                   *  to increment a semaphore's count by a specified amount. The count can never be less
00048                   *   than zero or greater than the value specified in the maxCount parameter.
00049                   * \ingroup synch_grp
00050                   */
00051                 class BASE_IMPEXP CSemaphore
00052                 {
00053                 protected:
00054                         utils::CReferencedMemBlock              m_data;
00055                         std::string  m_name; //!< The name of the named semaphore, or empty if unnamed.
00056 
00057         public:
00058             /** Creates a semaphore.
00059               *  If \a name is not an empty string, a named semaphore is created. In that case
00060               *   if the semaphore didn't exist it's created. Otherwise, the existing semaphore
00061               *   is linked to this object and then \a initialCount and \a maxCount are ignored.
00062               */
00063             CSemaphore(
00064                 unsigned int    initialCount,
00065                 unsigned int    maxCount,
00066                 const std::string &name=std::string("") );
00067 
00068             /** Destructor
00069               */
00070             virtual ~CSemaphore();
00071 
00072             /** Blocks until the count of the semaphore to be non-zero.
00073               * \param timeout_ms The timeout in milliseconds, or set to zero to wait indefinidely.
00074               * \return true if the semaphore has been signaled, false on timeout or any other error.
00075               */
00076             bool waitForSignal( unsigned int timeout_ms = 0 );
00077 
00078             /** Increments the count of the semaphore by a given amount.
00079               */
00080             void release(unsigned int increaseCount = 1);
00081 
00082 
00083                         /** Get the name of the named semaphore or an empty string if it's unnamed */
00084                         inline std::string  getName() const { return m_name; }
00085 
00086                         /** Return true if this is a named semaphore */
00087                         inline bool isNamed() const { return !m_name.empty(); }
00088 
00089 
00090                 };
00091 
00092         } // End of namespace
00093 
00094 } // End of namespace
00095 
00096 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011