Click here to Skip to main content
Click here to Skip to main content

Cross Platform Mutex

, 25 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Using Mutexes on cross platform programming

Introduction

This is my first article so .... here I go. Smile | :)

Using the code

Mutual exclusion (often abbreviated to mutex) is used in concurrent programming to avoid the simultaneous use of a common resource.

An example in wich you can use a mutex is in a multithreaded application when inserting items into a list.

The mutex implementation in windows and linux programming is a little different but with a little coding you can use the same thing for both.

To do this we will use macros like this:

			#if defined(LINUX)
			..... //do this if it is for linux
			#elif defined(WINDOWS)
			..... //do this if it is for windows
			#endif
		

To use cross platform we will use a header file and a .cpp file

Let's start with the header file. The first thing we must do is to include the header files for the mutex. This files are different for windows and linux. Here is the code:

			//cross.h
			#if defined(LINUX)
				#include <span class="code-keyword"><pthread.h></span>
			#elif defined(WINDOWS)
				#include <span class="code-keyword"><windows.h></span>
				#include <span class="code-keyword"><process.h></span>
			#endif
		

In windows the mutex is defined as HANDLE data type while in linux we have pthread_mutex_t for the mutex.

The code should now look like this:

			//headers
			#if defined(LINUX)
				#include <span class="code-keyword"><pthread.h></span>
			#elif defined(WINDOWS)
				#include <span class="code-keyword"><windows.h></span>
				#include <span class="code-keyword"><process.h></span>
			#endif

			//data types
			#if defined(LINUX)
				#define MUTEX pthread_mutex_t
			#elif defined(WINDOWS)
				#define MUTEX HANDLE
			#endif

Now we need to declare the functions that we'll use: init lock and unlock muetx. The full could should now look like this:

			//Headers
			#if defined(LINUX)
				#include <span class="code-keyword"><pthread.h></span>
			#elif defined(WINDOWS)
				#include <span class="code-keyword"><windows.h></span>
				#include <span class="code-keyword"><process.h></span>
			#endif

			//Data types
			#if defined(LINUX)
				#define MUTEX pthread_mutex_t
			#elif defined(WINDOWS)
				#define MUTEX HANDLE
			#endif

			//Functions
			int MUTEX_INIT(MUTEX *mutex);
			int MUTEX_LOCK(MUTEX *mutex);
			int MUTEX_UNLOCK(MUTEX *mutex);

Our functions will be held by the cross.cpp file:


int MUTEX_INIT(MUTEX *mutex)
{
    #if defined(LINUX)
        return pthread_mutex_init (mutex, NULL);;
    #elif defined(WINDOWS)
        *mutex = CreateMutex(0, FALSE, 0);;
        return (*mutex==0);
    #endif
    return -1;
}

int MUTEX_LOCK(MUTEX *mutex)
{
    #if defined(LINUX)
        return pthread_mutex_lock( mutex );
    #elif defined(WINDOWS)
        return (WaitForSingleObject(*mutex, INFINITE)==WAIT_FAILED?1:0);
    #endif
    return -1;
}

int MUTEX_UNLOCK(MUTEX *mutex)
{
    #if defined(LINUX)
        return pthread_mutex_unlock( mutex );
    #elif defined(WINDOWS)
        return (ReleaseMutex(*mutex)==0);
    #endif
    return -1;
}

I will not get into the details of this functions, you can use msdn for windows and mkssoftware for linux of you desire more informations.

Using the code

First of all you need to define WINDOWS or LINUX so the compiler knows the platform. This is all you need to change when switching from windows to linux. The code for mutexes is the same, you use it like this:

//declare the mutex:
MUTEX mutex;
//Initialize the mutex:
MUTEX_INIT(&mutex);
//Lock the mutex:
MUTEX_LOCK(&mutex);
//Unlock the mutex:
MUTEX_UNLOCK(&mutex);

Conclusion

I hope you find this article to be of some use.

Article powered by Multiplex Games

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gulyan

Romania Romania
No Biography provided

Comments and Discussions

 
Generalcross platform application Pinmemberros8828-Apr-09 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Gulyan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid