ThreadLib Mutexes

[ LiB ]

ThreadLib Mutexes

I've been playing around with threading, and I feel that the only synchronization structures that the ThreadLib really needs are mutexes, so that's all it has. There really was no reason to implement semaphores or conditions, because I don't use them. Anyway, the code for the mutex is located in the file /Libraries/ThreadLib/ThreadLibMutex.h on the CD. It's a pretty easy class to implement, because it requires only four functions.

There is one important thing I would like to mention before going on to show you the code. WIN32 has a structure called a mutex, and you might think I would use it in the ThreadLib, but it turns out that WIN32 mutexes are completely unnecessary for my purposes. You see, a WIN32 mutex is a heavyweight threading object, which means it can be used across processes, between different programs. Implementing this capability requires extra overhead though, and we only need lightweight mutex objectsobjects that can be used across different threads, but always within the same process. The pthreads library for Linux is lightweight in nature; it deals only with threads within a single process. So what about WIN32? Well, WIN32 has an object known as a critical section , which is just a lightweight mutex. So, for the thread library, I'll use critical sections instead of heavyweight mutexes.

Data

I always like to show the class without functions first, just to give you an idea of what data the class is going to store. So without further ado, here it is:

 class Mutex { protected: // define the base mutex types #ifdef WIN32     CRITICAL_SECTION m_mutex; #else     pthread_mutex_t m_mutex; #endif };  // end class Mutex 

In WIN32, the class has a CRITICAL_SECTION object, and in Linux, the class has a pthread_mutex_t object.

Constructor

Both APIs require that mutex objects be initialized before they are used, and it's logical to do so within the constructor:

 Mutex() {     #ifdef WIN32         // use critical sections in Windows; much faster         InitializeCriticalSection( &m_mutex );     #else         pthread_mutex_init( &m_mutex, 0 );     #endif } 

The only important point to note is the second parameter for the Linux function; it's supposed to be a pointer to an attributes structure, but that's not really needed. I pass in 0, meaning that the default attributes of a mutex are applied.

Destroying a Mutex

When you're finished with mutexes, they need to be destroyed . The destructor does this automatically for you:

 ~Mutex() {     #ifdef WIN32         DeleteCriticalSection( &m_mutex );     #else         pthread_mutex_destroy( &m_mutex );     #endif } 

Locking a Mutex

As with destroying mutexes, locking mutexes is a simple task:

 inline void Lock() {     #ifdef WIN32         EnterCriticalSection( &m_mutex );     #else         pthread_mutex_lock( &m_mutex );     #endif } 

Unlocking a Mutex

Unlocking a mutex is also a simple task:

 inline void Unlock() {     #ifdef WIN32         LeaveCriticalSection( &m_mutex );     #else         pthread_mutex_unlock( &m_mutex );     #endif } 

See, I told you this would be easy! That's it for the code.

[ LiB ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net