1dispatch_once(3) BSD Library Functions Manual dispatch_once(3)
2
4 dispatch_once — execute a block only once
5
7 #include <dispatch/dispatch.h>
8
9 void
10 dispatch_once(dispatch_once_t *predicate, void (^block)(void));
11
12 void
13 dispatch_once_f(dispatch_once_t *predicate, void *context,
14 void (*function)(void *));
15
17 The dispatch_once() function provides a simple and efficient mechanism to
18 run an initializer exactly once, similar to pthread_once(3). Well
19 designed code hides the use of lazy initialization. For example:
20
21 FILE *getlogfile(void)
22 {
23 static dispatch_once_t pred;
24 static FILE *logfile;
25
26 dispatch_once(&pred, ^{
27 logfile = fopen(MY_LOG_FILE, "a");
28 });
29
30 return logfile;
31 }
32
34 The dispatch_once() function is a wrapper around dispatch_once_f().
35
37 dispatch(3)
38
39Darwin May 1, 2009 Darwin