Which are the different ways of Thread Synchronization ?

 
 

In a multi threaded application each thread must coordinate its actions with other threads in the application. If two or more threads access a same resource then that resource needs to be synchronized so that all threads don’t try to modify it at the same time.
Windows supports four types of synchronization objects that can be used to synchronize the actions performed by concurrently running multiple threads. 
-         Critical Sections
-         Mutexes
-         Events
-         Semaphores
Critical Section: These are used to serialize access to resources that are accessed by multiple threads. All the threads must belong to same process because critical sections won’t work across process boundaries. CCriticalSection is a class and CCriticalSection::Lock locks a critical section and CCriticalSection::UnLock unlocks it. 
E.g. CCriticalSection cs;
//Thread T1
cs.Lock ();
//access the shared resource
cs.UnLock (); 
/Thread T2
cs.Lock ();
/access the shared resource
Cs.Unlock ();
Mutexes: Serves the same purpose as critical sections but can be used to synchronize threads running in same process or across processes. CMutex is its corresponding class. 
Semaphores: They maintain resource counts representing the number of resources available. Locking a semaphore decrements resource count and unlocking increments it. A thread that tries to lock a semaphore with resource count 0 gets blocked until another thread unlocks the semaphore and increases the resource count. CSemaphore is a class representing semaphores.