Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>NSMutableDictionary</code> isn't designed to be thread-safe data structure, and simply marking the property as <code>atomic</code>, doesn't ensure that the underlying data operations are actually performed atomically (in a safe manner).</p> <p>To ensure that each operation is done in a safe manner, you would need to guard each operation on the dictionary with a lock:</p> <pre><code>// in initialization self.dictionary = [[NSMutableDictionary alloc] init]; // create a lock object for the dictionary self.dictionary_lock = [[NSLock alloc] init]; // at every access or modification: [object.dictionary_lock lock]; [object.dictionary setObject:image forKey:name]; [object.dictionary_lock unlock]; </code></pre> <p>You should consider rolling your own <code>NSDictionary</code> that simply delegates calls to NSMutableDictionary while holding a lock:</p> <pre><code>@interface SafeMutableDictionary : NSMutableDictionary { NSLock *lock; NSMutableDictionary *underlyingDictionary; } @end @implementation SafeMutableDictionary - (id)init { if (self = [super init]) { lock = [[NSLock alloc] init]; underlyingDictionary = [[NSMutableDictionary alloc] init]; } return self; } - (void) dealloc { [lock_ release]; [underlyingDictionary release]; [super dealloc]; } // forward all the calls with the lock held - (retval_t) forward: (SEL) sel : (arglist_t) args { [lock lock]; @try { return [underlyingDictionary performv:sel : args]; } @finally { [lock unlock]; } } @end </code></pre> <p>Please note that because each operation requires waiting for the lock and holding it, it's not quite scalable, but it might be good enough in your case.</p> <p>If you want to use a proper threaded library, you can use <a href="http://transactionkit.sourceforge.net/" rel="noreferrer">TransactionKit library</a> as they have <code>TKMutableDictionary</code> which is a multi-threaded safe library. I personally haven't used it, and it seems that it's a work in progress library, but you might want to give it a try.</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