Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C
Article

Cross Platform Mutex

Rate me:
Please Sign up or sign in to vote.
2.71/5 (10 votes)
25 Apr 2008CPOL1 min read 40.9K   20   1
Using Mutexes on cross platform programming

Introduction

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

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 <pthread.h>
#elif defined(WINDOWS)
    #include <windows.h>
    #include <process.h>
#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 <pthread.h>
#elif defined(WINDOWS)
    #include <windows.h>
    #include <process.h>
#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 <pthread.h>
#elif defined(WINDOWS)
    #include <windows.h>
    #include <process.h>
#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)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalcross platform application Pin
ros8828-Apr-09 22:18
ros8828-Apr-09 22:18 

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

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