I'm wondering how I can make access to a __block
qualified var thread-safe within the context of a method.
Example:
__block NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (int i=0; i<20; i++) {
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[dictionary setObject:@"test" forKey:@"test"];
}];
[someConcurrentQueue addOperation:operation];
}
Here the operation is added to a concurrent queue and the dictionary
var will potentially be
accessed from different threads at the same time.
Is this safe? If not, how do I make access to dictionary
safe?