Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

mutex.h

Go to the documentation of this file.
00001 
00003 
00004 #ifndef header_mutex
00005 #define header_mutex
00006 
00007 class CL_Mutex
00008 //: Mutex interface.
00009 // If you don't know what a mutex is, read a book. :-)
00010 {
00011 public:
00012         static CL_Mutex *create();
00013         // Call this to create a mutex.
00014 
00015         virtual ~CL_Mutex() { ; }
00016         
00017         virtual void enter()=0;
00018         // Enter the critical section.
00019 
00020         virtual void leave()=0;
00021         // Leave the critical section.
00022 
00023         virtual void wait()=0;
00024         //: wait releases the mutex this thread has taken out and sends the
00025         // thread to sleep. Other threads can then acquire the mutex and
00026         // modify any data. When another thread calls notify, the thread
00027         // that called wait will wake up again and automatically reacquire
00028         // the mutex.
00029         //
00030         // <b>Warning:</b> This function must not be called on an unlocked 
00031         // mutex. You must call enter first.
00032         //
00033         // <b>Warning:</b> When wait returns (after another thread has called
00034         // notify/notifyAll, the current thread will have a mutex lock. Be
00035         // sure to call leave at some point.
00036         //
00037         // <b>Warning:</b> Not implemented under Win32. If you need it,
00038         // please implement it in Sources/Core/System/Win32/mutex_win32.*,
00039         // and send me the patch. :-)
00040 
00041         virtual void notify()=0;
00042         //: Notify wakes up the first thread that has gone to sleep on this
00043         // mutex in order to wait for a notification. For safety reasons,
00044         // you are advised to acquire the mutex using 'enter' before calling
00045         // notify. (Of course you have to release the mutex using 'leave' 
00046         // after notifying, otherwise the waiting thread cannot reacquire it).
00047         //
00048         // <b>Warning:</b> Not implemented under Win32. If you need it,
00049         // please implement it in Sources/Core/System/Win32/mutex_win32.*,
00050         // and send me the patch. :-)
00051 
00052         virtual void notify_all()=0;
00053         //: NotifyAll wakes up all threads waiting on the mutex.
00054         // <b>Warning:</b> Not implemented under Win32. If you need it,
00055         // please implement it in Sources/Core/System/Win32/mutex_win32.*,
00056         // and send me the patch. :-)
00058 };
00059 
00060 class CL_MutexSection
00061 //: Lock a mutex until the end of a scope.
00062 // This class is a way to ensure a mutex will be released at the end of 
00063 // a scope. When an instance is constructed, it will lock the mutex, and
00064 // when the instance is destroyed (at the exit of its scope), it will
00065 // unlock the mutex.
00066 {
00067 public:
00068         CL_MutexSection(CL_Mutex *mutex)
00069         {
00070                 this->mutex = mutex;
00071                 mutex->enter();
00072         }
00073 
00074         virtual ~CL_MutexSection()
00075         {
00076                 mutex->leave();
00077         }
00078 
00079 private:
00080         CL_Mutex *mutex;
00081 };
00082 
00083 #endif

Generated at Wed Apr 4 19:54:02 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001