Windows Thread Synchronization 4

 

 

 

Synchronization Objects

 

A synchronization object is an object whose handle can be specified in one of the wait functions to coordinate the execution of multiple threads. More than one process can have a handle to the same synchronization object, making interprocess synchronization possible. The following object types are provided exclusively for synchronization.

 

Type

Description

Event

Notifies one or more waiting threads that an event has occurred. Events are a way of signaling one thread from another, allowing one thread to wait or sleep until it’s signaled by another thread.

Mutex

Can be owned by only one thread at a time, enabling threads to coordinate mutually exclusive access to a shared resource.

Semaphore

Maintains a count between zero and some maximum value, limiting the number of threads that are simultaneously accessing a shared resource. A semaphore is a mutex that multiple threads can access. It’s like having multiple tokens. Mutex is exactly the same as a semaphore with semaphore value 1.

Waitable timer

Notifies one or more waiting threads that a specified time has arrived.

 

 

 

Events

 

Events are a way of signaling one thread from another, allowing one thread to wait or sleep until it’s signaled by another thread. The following example shows two threads using an event: Thread A on the left is producing data, and Thread B on the right is consuming data.

 

Windows thread synchronization: The event object

The producing thread, Thread A generates some data and puts it in a shared working space. In this example, the consuming thread, Thread B is sleeping on the event (waiting for the event to trigger).

 

Windows thread synchronization: Event triggered

Once the producing thread has finished writing data, it triggers the event.

 

Windows thread synchronization: Telling the event the data already sent

This signals the consuming thread, Thread B, thereby waking it up.

 

Windows thread synchronization: Event - telling other thread to use the sent data

 

Once the consuming thread, Thread B has woken up, it starts doing work. The assumption is that the producing thread will no longer touch the data.

 

Windows thread synchronization: Event - other thread start using the data

 

 

 

< Thread Synchronization 3 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 5 >