Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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.
0
fattien
Asked:
fattien
1 Solution
 
ahoffmannCommented:
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
 
FlamingSwordCommented:
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
 
FlamingSwordCommented:
How about Linux?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
SunBowCommented:
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
 
SunBowCommented:
?
0
 
FlamingSwordCommented:
.
0
 
BlackDiamondCommented:
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
 
laaxCommented:
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
 
jhellzenCommented:
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
 
Leo71Commented:
@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
 
locomojoCommented:
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
 
bkrahmerCommented:
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
 
SpideyModCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now