CVR-Lib last update 20 Sep 2009

cvrMutex.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2005
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrMutex.h
00044  *         Contains cvr::mutex for protecting critical sections for
00045  *         multithreading.
00046  * \author Thomas Rusert
00047  * \date   04.11.1999
00048  *
00049  * $Id: cvrMutex.h,v 1.6 2005/01/03 16:16:22 alvarado Exp $
00050  */
00051 
00052 #ifndef _CVR_MUTEX_H_
00053 #define _CVR_MUTEX_H_
00054 
00055 #include "cvrMacroSymbols.h"
00056 
00057 #ifndef _CVR_WIN32
00058 #  include <pthread.h> // for unix/linux systems (posix threads!)
00059 #else
00060 #  include <windows.h>
00061 #  include <process.h>
00062 #endif
00063 
00064 namespace cvr {
00065   /**
00066    * Mutex class for the library
00067    *
00068    * This object can be used to protect critical sections on multithreaded
00069    * applications.  The same thread should NOT try to lock the mutex more than
00070    * once.  The behavior of this will depend on the operating system: on
00071    * linux/unix the thread will be locked forever (posix standard); on
00072    * windows, the thread will count how many lock have been done, but it will
00073    * not be blocked by the later locks!
00074    *
00075    * Example:
00076    *
00077    * \code
00078    * // A class with some code to be protected
00079    * class A {
00080    * private:
00081    *   // The mutex used to protect the some code blocks
00082    *   cvr::mutex lock_;
00083    *
00084    *   // Data that requires exclusive access, e.g. a std::list
00085    *   std::list<int> data_;
00086    *
00087    * public:
00088    *   // A method that requires some exclusive access
00089    *   void access() {
00090    *     lock_.lock();   // ensure exclusive access to the data
00091    *     static int numAccesses = 0;
00092    *     data_.push_back(numAccesses++);
00093    *     lock_.unlock(); // realease exclusive access
00094    *   }
00095    * };
00096    * \endcode
00097    *
00098    * In the previous example the "access()" method ensures that only one thread
00099    * at a time has access to the data_ attribute of the class.  Other methods
00100    * that also access the data_ attribute should also protect the access.
00101    *
00102    * @see cvr::semaphore, cvr::thread
00103    */
00104   class mutex {
00105   public:
00106     /**
00107      * Default constructor with unlocked mutex.
00108      */
00109     mutex();
00110 
00111     /**
00112      * Destructor
00113      */
00114     virtual ~mutex();
00115 
00116     /**
00117      * Wait until lock for mutex becomes available and lock it
00118      */
00119     void lock();
00120 
00121     /**
00122      * Try to lock mutex, but do not block.
00123      *
00124      * @return true if locking was successful.
00125      */
00126     bool tryLock();
00127 
00128     /**
00129      * Unlock mutex
00130      */
00131     void unlock();
00132 
00133   protected:
00134     /**
00135      * Destroy the mutex
00136      */
00137     void destroy();
00138 
00139 # ifndef _CVR_WIN32
00140     /**
00141      * the posix mutex object
00142      */
00143     pthread_mutex_t theMutex_;
00144 # else
00145     /**
00146      * the WIN32 mutex objects
00147      */
00148     HANDLE theMutex_;
00149 # endif
00150   };
00151 }
00152 
00153 #endif
00154 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8