This manual page is part of Xcode Tools version 4.0

To obtain these tools:

If you are running a version of Xcode Tools other than 4.0, view the documentation locally:

  • In Xcode

  • In Terminal, using the man(1) command

Reading manual pages

Manual pages are intended as a quick reference for people who already understand a technology.

  • To learn how the manual is organized or to learn about command syntax, read the manual page for manpages(5).

  • For more information about this technology, look for other documentation in the Apple Developer Library.

  • For general information about writing shell scripts, read Shell Scripting Primer.




dispatch_semaphore_cr... BSD Library Functions Manual dispatch_semaphore_cr...

NAME
     dispatch_semaphore_create, dispatch_semaphore_signal, dispatch_semaphore_wait -- synchronized counting
     semaphore

SYNOPSIS
     #include <dispatch/dispatch.h>

     dispatch_semaphore_t
     dispatch_semaphore_create(long count);

     long
     dispatch_semaphore_signal(dispatch_semaphore_t semaphore);

     long
     dispatch_semaphore_wait(dispatch_semaphore_t semaphore, dispatch_time_t timeout);

DESCRIPTION
     Dispatch semaphores are used to synchronize threads.  The timeout parameter is creatable with the
     dispatch_time(3) or dispatch_walltime(3) functions.

COMPLETION SYNCHRONIZATION
     If the count parameter is equal to zero, then the semaphore is useful for synchronizing completion of
     work.  For example:

           sema = dispatch_semaphore_create(0);

           dispatch_async(queue, ^{
                   foo();
                   dispatch_semaphore_signal(sema);
           });

           bar();

           dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

FINITE RESOURCE POOL
     If the count parameter is greater than zero, then the semaphore is useful for managing a finite pool of
     resources.  For example, a library that wants to limit Unix descriptor usage:

           sema = dispatch_semaphore_create(getdtablesize() / 4);

     At each Unix FD allocation:

           dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
           fd = open("/etc/services", O_RDONLY);

     When each FD is closed:

           close(fd);
           dispatch_semaphore_signal(sema);

RETURN VALUES
     The dispatch_semaphore_create() function returns NULL if no memory is available or if the count parame-ter parameter
     ter is less than zero.

     The dispatch_semaphore_signal() function returns non-zero when a thread is woken.  Otherwise, zero is
     returned.

     The dispatch_semaphore_wait() function returns zero upon success and non-zero after the timeout
     expires. If the timeout is DISPATCH_TIME_FOREVER, then dispatch_semaphore_wait() waits forever and
     always returns zero.

MEMORY MODEL
     Dispatch semaphores are retained and released via calls to dispatch_retain() and dispatch_release().

CAVEATS
     Unbalanced dispatch semaphores cannot be released.  For a given semaphore, calls to
     dispatch_semaphore_signal() and dispatch_semaphore_wait() must be balanced before dispatch_release() is
     called on it.

SEE ALSO
     dispatch(3), dispatch_object(3)

Darwin                            May 1, 2009                           Darwin

Reporting Problems

The way to report a problem with this manual page depends on the type of problem:

Content errors
Report errors in the content of this documentation with the feedback links below.
Bug reports
Report bugs in the functionality of the described tool or API through Bug Reporter.
Formatting problems
Report formatting mistakes in the online version of these pages with the feedback links below.

Did this document help you? Yes It's good, but... Not helpful...