latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiMutex.h 00027 * authors ....: Thomas Rusert 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 04.11.99 00030 * revisions ..: $Id: ltiMutex.h,v 1.4 2006/02/08 12:53:49 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_MUTEX_H_ 00034 #define _LTI_MUTEX_H_ 00035 00036 #include "ltiObject.h" 00037 00038 #ifndef _LTI_WIN32 00039 # include <pthread.h> // for unix/linux systems (posix threads!) 00040 #else 00041 # include <windows.h> 00042 # include <process.h> 00043 #endif 00044 00045 namespace lti { 00046 /** lti mutex class 00047 00048 This object can be used to protect critical sections on 00049 multithreaded applications. The same thread should NOT try 00050 to lock the mutex more than once. The behavior of this will 00051 depend on the operating system: on linux/unix the thread will 00052 be locked forever (posix standard); on windows, the thread 00053 will count how many lock have been done, but it will not be 00054 blocked by the later locks! 00055 00056 @see lti::semaphore, lti::thread 00057 */ 00058 class mutex : public object { 00059 public: 00060 /// default constructor 00061 mutex(); 00062 /// destructor 00063 virtual ~mutex(); 00064 /// wait until lock for mutex becomes available and lock it 00065 void lock(); 00066 /** Try to lock mutex, but do not block. 00067 @return true if locking was successful. */ 00068 bool tryLock(); 00069 /// unlock mutex 00070 void unlock(); 00071 00072 /// returns the name of this type 00073 virtual const char* getTypeName() const; 00074 protected: 00075 void destroy(); 00076 00077 # ifndef _LTI_WIN32 00078 /// the posix mutex object 00079 pthread_mutex_t theMutex; 00080 # else 00081 /// the WIN32 mutex objects 00082 HANDLE theMutex; 00083 # endif 00084 }; 00085 } 00086 00087 #endif