fattien
asked on
Critical Sections
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.
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.
C/C++ doesn't support "synchronization" stuff as Critical Sections, Mutex .... . You need to use a platform dependent solution for "multi thread" .
WINDOZZZZZZZE
/* either have this global, a class member or pass it to the function(s) that will use it */
HANDLE mutex = CreateMutex(NULL,FALSE,NUL L)
void my_fn()
{
if(WaitForSingleObject(mut ex,my_time out_in_mil lis /* 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..........
/* either have this global, a class member or pass it to the function(s) that will use it */
HANDLE mutex = CreateMutex(NULL,FALSE,NUL
void my_fn()
{
if(WaitForSingleObject(mut
{
// 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..........
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are using pthread, then the function you are looking for pthread_mutex_lock.