Solved

Critical Sections

Posted on 2002-04-16
5
301 Views
Last Modified: 2010-04-15
I'm writting a C program which is supposed to run on multiple platforms (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 platform dependent solution.
0
Comment
Question by:fattien
5 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6945190
Yes, you can use mutexes in C.  It is dependant on the thread library that you are using as to how you implement them.  What thread library are you using?

If you are using pthread, then the function you are looking for pthread_mutex_lock.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6945729
C/C++ doesn't support "synchronization" stuff as Critical Sections, Mutex .... . You need to use a platform dependent solution for "multi thread" .
0
 
LVL 10

Expert Comment

by:makerp
ID: 6947263
WINDOZZZZZZZE

/* either have this global, a class member or pass it to the function(s) that will use it */
HANDLE mutex = CreateMutex(NULL,FALSE,NULL)

void my_fn()
{
  if(WaitForSingleObject(mutex,my_timeout_in_millis /* use the INFINITE constant to wait for ever */) == WAIT_OBJECT_0)
  {
    // do your stuff here
    ReleaseMutex(mutex);
  }
  else
  {
    // your request for the mutex timed out
  }
}

it will be very similar on other platforms. if the mutex only need local process scope then use CRITICAL_SECTION instead as they are more light weight. these have the same semantics and are similar to use..........

0
 
LVL 1

Expert Comment

by:arul_m
ID: 6953623
It is possible to implement Mutex independent of platform by locking an OS resource with platform independent C Runtime API. I have implemented mutex by locking/unlocking the file. Find below the code skeleton for the same:

Mutex Locking:
^^^^^^^^^^^^^^
while(true)
{
 /* Try to remove the 'locked' file.
    This will throw an error if a
    thread/process has already opened
    the file */

  if (remove(filename)==0)
    break;

  // Wait for a while before trying again
  sleep(delay);
}
file = fopen(filename,"w");


Unlocking the mutex:
^^^^^^^^^^^^^^^^^^^
fclose(file);
file = NULL;

In the above example, you need to share the 'filename' across threads.
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8279760
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 134
Why this code doesn't work? 8 93
Estimate porting issues. 9 116
how to understand recursion 12 206
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

743 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

12 Experts available now in Live!

Get 1:1 Help Now