Link to home
Start Free TrialLog in
Avatar of fattien
fattien

asked on

Critical Sections

I'm writting a C program which is supposed to run on multiple plateforms (Windows, AIX ,..),This program is multithreaded, There are sections in the program that i want to restrict only one thread at a time to execute those sections(Critical Sections), I know that the solution is to use MUTEX.
Anyone can tell me wether the language C supports MUTEX or Not???
if yes , Tell me the details.
or if anyone has a different solution, pls tell me.
I want to confirm that i can't use a plateform dependent solution.
Avatar of ahoffmann
ahoffmann
Flag of Germany image

multithreading is always platform dependent.
It has also nothing to do with C (and other) language, you need a library which gives you the appropriate functions and data structures.

If you find such a library which supports Windoze and AIX, you code might be straight forward and without platform specific defines.
Avatar of FlamingSword
FlamingSword

Mutual Exclusion... (MutEx)
hmmm..
I'll wait, you go.
No, you go first, I'll wait.
no, that's ok, you can go first.
Oh, I don't mind going next...
How about Linux?
So what happens when there are two rails side by side, approaching a hill, but only one rail connecting through the tunnel in between? (cost savings)
The pthread library is supported on Windows, AIX, Linux, BSD and many others.  There is lots of documentation around on the pthread library, as it has become the defacto standard cross-platform thread implementation.
1. MutEx is not supported in C language.
2. Go thru the following implementation step.
   1. When a thead wants to execute a method contains
Critical Section, Call sleep() for a short duration.
This avoids two thread entering the CS at the same time.
After sleep() check for the flag ( set by any thread
already entered CS).  

   2. If the flag is set...wait for some more time(for random milli seconds) and check again....

   3. If the flag is reset, set the flag first and go ahead and call the method / function.

   3. The thread exiting CS, should reset the flag.

  This solution is platform independent.

Laax.
FYI:
for a multi-platform toolkit to do what you propose see:
http://www.cs.wustl.edu/~schmidt/ACE.html

Dozens of environments and a damn good (free) product used by lots and lots of developers.

It will do mutexs, barriers, semaphores, critical sections, etc. in a platform independant manner.
@laax: I prefer having an atomic exchange instruction. So that you can forget about the Sleep() and are 100% water proofed ;-).
Unfortunately I don't know a C exchange instrucition :-( (I'm really missing that).
By the way I hope there is no sceduler that puts a thread into run state, let it do one instruction (read flag) and than puts it into wait state again. That would let your algorithm fail (and as I know Murphy if there is the posibility...)..
:)
Regards
Leo
You should use POSIX mutexes for the UNIX platforms and Critical Sections for Win32.

use:

int Lock()
{
int iResult;
#ifdef WIN32
iResult = EnterCriticalSection(&g_MyCriticalSection);
#else // WIN32
iResult = pthread_mutex_lock(&g_MyCriticalSection);
#endif // WIN32
}

int UnLock()
{
#ifdef WIN32
iResult = LeaveCriticalSection(&g_MyCriticalSection);
#else // WIN32
iResult = pthread_mutex_unlock(&g_MyCriticalSection);
#endif // WIN32
}

Where g_MyCriticalSection is defined according to the platform you use.

Bear in mind that the Windows Critical Sections are Recursive while the POSIX are by default not so Create Posix Mutexes Recursive for the same behavior.

Hope it helps.

Cheers.
The last three answers are more correct.  We're using a method similar to locomojo's approach on a large cross-platform app that is heavily threaded.  It works.  Ofc, it's much nicer in C++ as I can cleanly encapsulate objects such as Thread, Mutex, Event, etc.

brian
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial