dispatch_after(3)

๐Ÿ“ฆ
version 6.2.2 ยท 2 versions
  • 6.2.2 ยท alpine 3.24, arch rolling, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, opensuse-leap 16.0 (latest)
  • 0~svn197 ยท ubuntu jammy
, in alpine 3.24, arch rolling, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, opensuse-leap 16.0

๐Ÿ’– dispatch_after(3) โ€” the "do it later bestie" manual page ๐Ÿ’–

okay so welcome to dispatch_after, a member of the Library Functions Manual family, and honestly? she's that girl. she schedules blocks for deferred execution, which is a fancy way of saying "bestie, not right now, but at a specific vibe-checked moment later." โœจ


๐ŸŒธ NAME ๐ŸŒธ

dispatch_after โ€” schedule blocks for deferred execution ๐Ÿ•๐Ÿ’…


โœจ SYNOPSIS โœจ

first things first, you gotta bring in the header:

#include <dispatch/dispatch.h>

then here's the two glow-ups available to you:

void
dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
    void (^block)(void));

void
dispatch_after_f(dispatch_time_t when, dispatch_queue_t queue,
    void *context, void (*function)(void *));

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so what does dispatch_after() actually do? she submits the block to the queue you hand her, at the time specified by the when parameter. that's it, that's the whole job, and she does it flawlessly. ๐Ÿ‘‘

quick note on when: it's a value you create with dispatch_time() or dispatch_walltime(). don't just make up a number, bestie, use the actual functions. ๐Ÿซถ

now, real talk: submission of the block may be delayed by the system in order to improve power consumption and system performance. so if your block shows up a little late, it's not ghosting you, it's the system being โœจefficientโœจ. the system applies a leeway (peep dispatch_source_set_timer(3)) that is equal to one tenth of the interval between when and the time at which the function is called, and that leeway is capped to at least one millisecond and at most one minute. math! iconic. ๐Ÿงฎ๐Ÿ’–

and if you want the whole deep dive on submitting blocks to queues, go read dispatch_async(3) โ€” she's the main character of that topic, fr fr.


๐Ÿ’€ CAVEATS ๐Ÿ’€

okay listen up because this part matters, no cap:

  • dispatch_after() retains the passed queue. so the queue is staying alive whether you like it or not. she's not leaving. ๐Ÿ’…
  • specifying DISPATCH_TIME_NOW as the when parameter is supported, but it is not as efficient as just calling dispatch_async(). so like, technically valid, but why would you do that to yourself. ๐Ÿ˜ญ
  • passing DISPATCH_TIME_FOREVER as the when parameter? yeah the result of that is undefined. do not. that's not a vibe, that's a bug waiting to happen. ๐Ÿ™ˆ

๐ŸŽ€ FUNDAMENTALS ๐ŸŽ€

fun fact for your notes app: dispatch_after() is literally just a wrapper around dispatch_after_f(). she's wearing a cute little coat over her more hardcore sibling. the way that she's built different but also built the same. ๐Ÿงš


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

go give these queens a read too:


reminder: keep every when value real, keep your leeway expectations reasonable, and never ever pass DISPATCH_TIME_FOREVER. i'm looking at you. ๐Ÿ‘€

and that's the page, bestie! ๐ŸŽ€ this one's brought to you by Darwin, dated May 1, 2009 โ€” yes she's been serving since 2009 and she's still relevant. periodt. ๐Ÿ’–โœจ๐Ÿฆ‹