semaphore.h

Introduction

Use the links in the table of contents to the left to access the documentation.



Functions

dispatch_semaphore_create

Creates new counting semaphore with an initial value.

dispatch_semaphore_signal

Signal (increment) a semaphore.

dispatch_semaphore_wait

Wait (decrement) for a semaphore.


dispatch_semaphore_create


Creates new counting semaphore with an initial value.

(
    macos(
        10.6),
    ios(
        4.0)) DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED   dispatch_semaphore_t dispatch_semaphore_create(
    intptr_t value);  
Parameters
value

The starting value for the semaphore. Passing a value less than zero will cause NULL to be returned.

Return Value

The newly created semaphore, or NULL on failure.

Discussion

Passing zero for the value is useful for when two threads need to reconcile the completion of a particular event. Passing a value greater than zero is useful for managing a finite pool of resources, where the pool size is equal to the value.


dispatch_semaphore_signal


Signal (increment) a semaphore.

(
    macos(
        10.6),
    ios(
        4.0))  intptr_t dispatch_semaphore_signal(
    dispatch_semaphore_t dsema);  
Parameters
dsema

The counting semaphore. The result of passing NULL in this parameter is undefined.

Return Value

This function returns non-zero if a thread is woken. Otherwise, zero is returned.

Discussion

Increment the counting semaphore. If the previous value was less than zero, this function wakes a waiting thread before returning.


dispatch_semaphore_wait


Wait (decrement) for a semaphore.

(
    macos(
        10.6),
    ios(
        4.0))  intptr_t dispatch_semaphore_wait(
    dispatch_semaphore_t dsema,
    dispatch_time_t timeout);  
Parameters
dsema

The semaphore. The result of passing NULL in this parameter is undefined.

timeout

When to timeout (see dispatch_time). As a convenience, there are the DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.

Return Value

Returns zero on success, or non-zero if the timeout occurred.

Discussion

Decrement the counting semaphore. If the resulting value is less than zero, this function waits for a signal to occur before returning.


Typedefs

dispatch_semaphore_t

A counting semaphore.


dispatch_semaphore_t


A counting semaphore.

typedef struct dispatch_semaphore_s *dispatch_semaphore_t;