?
Solved

Critical Sections

Posted on 2002-04-16
13
Medium Priority
?
482 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:fattien
13 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6945457
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.
0
 
LVL 3

Expert Comment

by:FlamingSword
ID: 6946357
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...
0
 
LVL 3

Expert Comment

by:FlamingSword
ID: 6948142
How about Linux?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 24

Expert Comment

by:SunBow
ID: 6949252
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)
0
 
LVL 24

Expert Comment

by:SunBow
ID: 6951682
?
0
 
LVL 3

Expert Comment

by:FlamingSword
ID: 6954272
.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6960990
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.
0
 
LVL 1

Expert Comment

by:laax
ID: 7131671
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.
0
 
LVL 2

Expert Comment

by:jhellzen
ID: 7283266
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.
0
 

Expert Comment

by:Leo71
ID: 7950359
@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
0
 

Expert Comment

by:locomojo
ID: 8023401
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.
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8223323
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
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8279298
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question