Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you want is a simple global lock, look at NSLock.</p> <p>eg:</p> <pre><code>static NSLock * myGlobalLock = [[NSLock alloc] init]; if ([myGlobalLock tryLock]) { // do something [myGlobalLock unlock]; } else { // couldn't acquire lock } </code></pre> <p>However, this is going to incur a performance penalty because it requires a kernel call. If you want to serialize access to a resource, using Grand Central Dispatch and a private queue is going to perform much better--these are scheduled without requiring a kernel interrupt.</p> <p>eg:</p> <pre><code>// alloc a dispatch queue for controlling access to your shared resource static dispatch_queue_t mySharedResourceQueue = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ mySharedResourceQueue = dispatch_queue_create("com.myidentifier.MySharedResourceQueue", DISPATCH_QUEUE_SERIAL); // pick a unique identifier }); // don't forget to call dispatch_release() to clean up your queue when done with it! // to serialize access to your shared resource and block the current thread... dispatch_sync(mySharedResourceQueue, ^{ // access my shared resource }); // or to access it asynchronously, w/o blocking the current thread... dispatch_async(mySharedResourceQueue, ^{ // access my shared resource }); </code></pre> <p>Dispatch queues are highly awesome things and if you're getting in to iOS development you should learn how to use them in order to make apps with great performance.</p> <p>There are different types of locks besides NSLock too. Read up on synchronization in the Threaded Programming reference for more info...</p> <p>Threaded Programming reference: <a href="https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i" rel="nofollow">https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i</a></p> <p>NSLock reference: <a href="https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSLock_Class/Reference/Reference.html" rel="nofollow">https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSLock_Class/Reference/Reference.html</a></p> <p>Grand Central Dispatch reference: <a href="https://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html" rel="nofollow">https://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload