5,420,997 members and growing! (14,410 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Cross Platform » General License: The Code Project Open License (CPOL)

Cross Platform Mutex

By Member 2145299

Using Mutexes on cross platform programming
C++ (VC6, VC7, VC7.1, VC8.0, C++), C++/CLI, C

Posted: 25 Apr 2008
Updated: 25 Apr 2008
Views: 2,634
Bookmarked: 2 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 2.54 Rating: 2.67 out of 5
0 votes, 0.0%
1
3 votes, 33.3%
2
5 votes, 55.6%
3
0 votes, 0.0%
4
1 vote, 11.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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)

About the Author

Member 2145299



Location: Romania Romania

Other popular Cross Platform articles:

  • Introduction to Mono - Your first Mono app
    The first in a series of articles about Mono. This article explains how to install Mono and shows how to compile your first Cross Platform application.
  • MONO: an alternative for the .NET framework
    This article presents possibilities for development of .NET applications running on operating systems other than Windows, using the MONO platform. Advantages and challenges will be presented. Also presented are some common issues encountered while developing applications using the .NET technology.
  • Embed ActiveX controls inside Java GUI
    With this your Java projects can take advantage of ActiveX controls and Office documents such as spreadsheets, charts, calendars, word processors, specialized graphics, and many more.
  • Introduction to Mono - ASP.NET with XSP and Apache
    The second article in a series of articles about Mono. This article explains how to host and serve ASP.NET Web Applications and Web Services on Linux using XSP and Apache with the help of Mono.
  • Phalanger, PHP for .NET: Introduction for .NET developers
    Phalanger is a PHP language compiler for the .NET Framework which introduces PHP as a first-class .NET citizen.

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Apr 2008
Editor:
Copyright 2008 by Member 2145299
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project