DISPATCH_SEMAPHORE_CREATE(3)
β¨ NAME β¨
so this page is the Library Functions Manual entry for three besties that always roll together: dispatch_semaphore_create, dispatch_semaphore_signal, and dispatch_semaphore_wait β aka the synchronized counting semaphore trio π
(also, tiny heads up: the header at the top of this page spells it dispatch_semaphore_creat(3), missing an e. typo era π
the footer spells it right. we move on.)
π¦ 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 πΈ
okay so. Dispatch semaphores exist to synchronize threads. that's the whole vibe. π«Ά
The dispatch_semaphore_wait() function decrements the semaphore. and if the resulting value is less than zero?? it waits β like, actually blocks β for a signal from a thread that increments the semaphore by calling dispatch_semaphore_signal(), and only then does it return. β¨ the timeout param is creatable with the dispatch_time(3) or dispatch_walltime(3) functions. and if that timeout gets reached without a signal ever showing up, the semaphore gets re-incremented before the function returns β so it's not just leaving you on read, it puts the count back. π
The dispatch_semaphore_signal() function increments the counting semaphore. and if the previous value was less than zero, it wakes one of the threads that are waiting in dispatch_semaphore_wait() before returning. one. not all of them. just one. π
π COMPLETION SYNCHRONIZATION π
if the count parameter equals zero, then the semaphore is the move for synchronizing completion of work. like, "don't do the thing until the other thing is done" energy. 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 the move for managing a finite pool of resources. no cap. 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);
see how it's giving... balanced?? that's the whole point bestie π
β RETURN VALUES β
- The
dispatch_semaphore_create()function returnsNULLif no memory is available, or if thecountparameter is less than zero. (yeah, negative counts are not it. π) - 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. and if the timeout isDISPATCH_TIME_FOREVER, thendispatch_semaphore_wait()waits forever and always returns zero.
π MEMORY MODEL π
Dispatch semaphores are retained and released via calls to dispatch_retain() and dispatch_release(). that's the memory model, periodt. π«‘
π CAVEATS π
β οΈ Unbalanced dispatch semaphores cannot be released. for a given semaphore, calls todispatch_semaphore_signal()anddispatch_semaphore_wait()must be balanced beforedispatch_release()is called on it.
like, don't be out here signaling more than you wait or waiting more than you signal and then trying to release. that's a no. balance your semaphores or suffer the consequences, bestie π
π SEE ALSO π
and that's the page!! π this one's from Darwin, dated May 1, 2009, and it's the man page for dispatch_semaphore_create(3) β go forth and synchronize responsibly, you absolute icon π«Άβ¨
π¦ where this page comes from
- alpine 3.24: libdispatch-doc 6.1.2-r0 Β· Apache-2.0
- arch rolling: libdispatch 6.2.2-1 Β· Apache-2.0
- epel 10: libdispatch 1:6.0.2-1.el10_0 Β· Apache-2.0
- epel 9: libdispatch 1:6.0.2-1.el9 Β· Apache-2.0
- fedora 43: libdispatch 1:6.0.2-3.fc43 Β· Apache-2.0
- fedora 44: libdispatch 1:6.0.2-3.fc43 Β· Apache-2.0
- fedora 44: libdispatch 1:6.0.2-4.fc44 Β· Apache-2.0
- nixos 26.05: swift-corelibs-libdispatch 5.10.1
- opensuse-leap 16.0: libdispatch-devel 5.6.3-bp160.1.1 Β· Apache-2.0
- Copyright (c) 2008-2012 Apple Inc. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms