Semaphores

GCD provides semaphores – objects that threads can wait on and signal with (where signal is not a Unix signal). There are two kinds of semaphores: binary semaphores and counting semaphores.

A binary semaphore is a single flag. The flag starts out signaled. Waiting on it claims the flag. Any further attempts to wait on it require blocking until it is signaled again.

A counting semaphore is often initialized with the count of some resource and then used to allocate that resource. Waiting on the semaphore claims a unit, and signaling releases that claim. Only when the units have run out (the semaphore’s count being zero) does waiting on the semaphore actually block, requiring waiting for a unit to become available. Waiting can block, ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.