Tutto sulla sincronizzazione in ambiente windows

Mutex
Semafori
Sezioni critiche

I Mutex sono binari e vengono usati essenzialmente per gestire mutue esclusioni. Non vi è un esplicito contatore associato al mutex, solo libero/occupato. A seguito di WaitforSingleObject multipli da parte di un thread (permessi dal SO e non bloccanti per evitare un deadlock) quello stesso thread deve chiamare uno stesso numero di ReleaseMutex per liberare il mutex. Esiste quindi un contatore implicito visibile solo all'interno del thread per gestire questa particolare situazione. Questo contatore implicito non è visibile agli altri thread e cessa di esistere quando il controllo è dato ad un altro thread.
After a thread obtains ownership of a mutex, it can specify the same mutex in repeated calls to the wait-functions without blocking its execution. This prevents a thread from deadlocking itself while waiting for a mutex that it already owns. To release its ownership under such circumstances, the thread must call ReleaseMutex once for each time that the mutex satisfied the conditions of a wait function.
I semafori invece hanno associato un numero intero >=0 e non un binario. Ogni WaitForSingleObject decrementa il contatore e ogni ReleaseSemaphore lo incrementa. Se il contatore è 0 allora il semaforo è occupato, libero se >0. Considera che nella terminologia MS "signaled" è inteso come libero.
Nel caso dei semafori il contatore è parte della risorsa semaforo ed è visibile a tutti i thread.
A thread that owns a mutex object can wait repeatedly for the same mutex object to become signaled without its execution becoming blocked. A thread that waits repeatedly for the same semaphore object, however, decrements the semaphore's count each time a wait operation is completed; the thread is blocked when the count gets to zero. Similarly, only the thread that owns a mutex can successfully call the ReleaseMutex function, though any thread can use ReleaseSemaphore to increase the count of a semaphore object.
Le sezioni critiche (Critical Section) sono del tutto simili ai mutex ma possono essere usati solo dai thread di uno stesso processo. Il mutex è interprocess (poi assegnare un nome al mutex per permettergli di essere referenziarlo da un altro processo!), la sezione critica è visibile solo all'interno di un unico processo ed è quindi più efficiente di un mutex.