Windows 8

Windows processes and threads

Every process contains one or more threads, and the Windows thread is the basic executable unit. Threads are scheduled on the basis of the usual factors: availability of resources such as CPUs and physical memory, priority, fairness, and so on.

 Windows has supported symmetric multiprocessing (SMP) since NT4, so threads can be allocated to separate processors within a system

Semaphores

semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore.

The semaphore object is useful in controlling a shared resource that can support a limited number of users. It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.

Methods

  • CreateSemaphore
  • WaitForSingleObject
  • ReleaseSemaphore

Critical Section

critical section object provides synchronization similar to that provided by a mutex object, except that a critical section can be used only by the threads of a single process. Event, mutex, and semaphore objects can also be used in a single-process application, but critical section objects provide a slightly faster, more efficient mechanism.

Methods

Mutex Object

You can use a mutex object to protect a shared resource from simultaneous access by multiple threads or processes. Each thread must wait for ownership of the mutex before it can execute the code that accesses the shared resource.

For example, if several threads share access to a database, the threads can use a mutex object to permit only one thread at a time to write to the database.

Mutex vs Critical Section

  • Mutexes can be shared between processes, but always result in a system call to the kernel which has some overhead.

  • Critical sections can only be used within one process, but have the advantage that they only switch to kernel mode in the case of contention

Windows 7

By Antonio Grimaldo

Windows 7

  • 587