Solved

Critical Sections

Posted on 2002-04-16
13
471 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
Comment Utility
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
Comment Utility
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
Comment Utility
How about Linux?
0
 
LVL 24

Expert Comment

by:SunBow
Comment Utility
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
Comment Utility
?
0
 
LVL 3

Expert Comment

by:FlamingSword
Comment Utility
.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:BlackDiamond
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now