dispatch_time(3)
π dispatch_time(3) β bestie edition π
okay so welcome to the dispatch_time(3) page from the Library Functions Manual, bestie π«Ά we're gonna talk about time (not in the existential way, in the scheduling way, slay).
β¨ NAME β¨
dispatch_time, dispatch_walltime β calculate temporal milestones π«
π SYNOPSIS π
#include <dispatch/dispatch.h>
static const dispatch_time_t DISPATCH_TIME_NOW = 0;
static const dispatch_time_t DISPATCH_TIME_FOREVER = ~0ull;
dispatch_time_t
dispatch_time(dispatch_time_t base, int64_t offset);
dispatch_time_t
dispatch_walltime(struct timespec *base, int64_t offset);
πΈ DESCRIPTION πΈ
So dispatch_time() and dispatch_walltime() are literally the easiest way to express temporal milestones β like, the "hey do this thing at this moment" points in time you hand to dispatch functions that need timeouts or run on a schedule. lowkey essential.
The dispatch_time_t type is a semi-opaque integer, and the only values that are externally defined are the special ones: DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER. Every other value gets represented using an internal format that is not safe for integer arithmetic or comparison, bestie. do NOT do math on it, do NOT compare it, just hold it and pass it along π
and heads up: the internal format is subject to change. no cap.
Now dispatch_time() returns a milestone relative to an existing milestone, after adding offset nanoseconds. and the way it works is: if the base parameter maps internally to a wall clock, then the value it returns is relative to the wall clock. otherwise, if base is DISPATCH_TIME_NOW, then the current time of the default host clock is used. periodt.
Meanwhile dispatch_walltime() is the one you want for creating a milestone relative to a fixed point in time using the wall clock β that's what the optional base parameter is for. and if base is NULL, then the current time of the wall clock is used. iconic.
π₯ EDGE CONDITIONS π₯
dispatch_time() and dispatch_walltime() are out here detecting overflow and underflow when they apply the offset parameter. they're paying attention, unlike me during lecture π
- Overflow causes
DISPATCH_TIME_FOREVERto be returned. and whenbaseisDISPATCH_TIME_FOREVER, then theoffsetparameter is ignored β like, straight up not read, it said "I'm busy". - Underflow causes the smallest representable value to be returned for a given clock.
π pls note: these are real edge conditions, not vibes. overflow β DISPATCH_TIME_FOREVER. underflow β smallest representable value for that clock. memorize it, it's giving exam question.
π¦ EXAMPLES π¦
Create a milestone two seconds in the future:
milestone = dispatch_time(DISPATCH_TIME_NOW, 2LL * NSEC_PER_SEC);
Create a milestone for use as an infinite timeout:
milestone = DISPATCH_TIME_FOREVER;
Create a milestone on Tuesday, January 19, 2038:
struct timespec ts;
ts.tv_sec = 0x7FFFFFFF;
ts.tv_nsec = 0;
milestone = dispatch_walltime(&ts, 0);
β RETURN VALUE β
These functions return an abstract value, and you use it with dispatch_after(), dispatch_group_wait(), or dispatch_semaphore_wait(). that's it, that's the whole return value. abstract. mysterious. main character energy π
π SEE ALSO π
and that's the whole page, bestie π this one came straight outta Darwin, dated May 1, 2009 β yeah it's been serving since 2009 and it still ate π β¨ mwah, go schedule something cute π
π¦ where this page comes from
- ubuntu jammy: libdispatch-dev 0~svn197-3.3ubuntu2
- Copyright (c) 2008-2009 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