Solved

Critical Sections

Posted on 2002-04-16
13
476 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 73
VBA Challenge how to code to loop through all files in folder and revise some part of the codes 15 76
collection output issue 9 59
IDE for Python 5 62
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

685 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