Dev:Singleton pattern

The modern approach to creating a singleton class is the following:

Or in Swift:

Note that  are lazy-loaded, so the implementation is identical to the Objective-C example.

Other Methods (Discouraged)
The traditional singleton creation is extremely inefficient because @synchronized not only lock a mutex, but also insert an exception handler (try/catch block). If you don't care about exceptions and willing to use C functions, pthread_once is a better alternative: pthread_once is implemented using a spin lock (which leads to a syscall_thread_switch kernel call in the worst case).

If it is safe to create multiple copies of the singleton and destroy the extra ones, you may even use CAS: Unfortunately, OSAtomicCompareAndSwapPtrBarrier on ARMv4 is implemented using a global spin lock, so in principle this is nowhere faster than pthread_once, and is even more error-prone. On ARMv6 it is implemented as a LDREX/STREX pair :