August 2011
Intermediate to advanced
552 pages
23h 48m
English
Sometimes you need to perform one-time initialization and have the work performed exactly one time, no matter how many threads are wanting that initialization to happen. This is often used for lazy initialization or for creating the One True Object when using singletons. There is a technique called Double-Checked Locking to do this, but it is very hard to get right. You can use pthread_once() to serialize a one-time initialization. You can also use dispatch_once().
void dispatch_once (dispatch_once_t *predicate, void (^block)(void));
static dispatch_once_t initializationPredicate; static blah *stuffToInitialize dispatch_once (&initializationPredicate, ...
Read now
Unlock full access