1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// Début du programme
int maxSessions = 10;
HANDLE sem = CreateSemaphore(NULL, maxSessions, maxSessions, "Global\\CompteurDeLicenses");
switch (WaitForSingleObject(sem , 0L))
{
case WAIT_OBJECT_0: // Pris en compte
// On peut continuer normalement
break;
case WAIT_TIMEOUT: // On a atteint le max
CloseHandle(sem);
throw Exception("Max atteint");
case WAIT_FAILED:
CloseHandle(sem);
RaiseLastOSError();
break;
} // Fin du switch
....
// Fin du programme
ReleaseSemaphore(sem, 1, NULL);
CloseHandle(sem); |
Partager