1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
int ma_fonction()
{
HANDLE hMutex;
DWORD dwWaitMutex;
hMutex=CreateMutex(NULL, FALSE, L"mon_mutex");
if(hMutex==NULL)
{
printf("Error while creating mutex handle");
return 0;
}
/* Test si le handle de mutex existe déjà */
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
/* on attend qu'il soit libéré */
dwWaitMutex=WaitForSingleObject(hMutex,INFINITE);
}
// Section critique
// appel de ma fonction à protéger
// Fin section critique
ReleaseMutex(hMutex);
CloseHandle(hMutex);
return 1;
} |