I have been using dispatch_once for shared instances but what i don't understand is how dispatch_once works, why it runs only once.
            Asked
            
        
        
            Active
            
        
            Viewed 472 times
        
    2 Answers
1
            
            
        Low level computing answer.
Different threads that try to access the critical section — the code passed to dispatch_once — while a thread is already in this section are blocked until the critical section completes.
there-can-only-be-one
It should be noted that this just makes access to the shared instance thread safe. It does not make the class thread safe, necessarily.
 
    
    
        caffieneToCode
        
- 602
- 5
- 15
0
            
            
        dispatch_once() is synchronous process and all GCD methods do things asynchronously (case in point, dispatch_sync() is synchronous)
The entire idea of dispatch_once() is "perform something once and only once", which is precisely what we're doing.
dispatch_once that’s used to guarantee that something happens exactly once, no matter how violent the program’s threading becomes
 
    
    
        Community
        
- 1
- 1
 
    
    
        user3182143
        
- 9,459
- 3
- 32
- 39
- 
                    This is an answer to what is dispatch_once() and what it does. There is no information for, "How it manages to run only once?" – caffieneToCode Feb 20 '19 at 05:33
