Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon queue for accessing shared resources in CGD
    primarykey
    data
    text
    <p>I have an array, <code>sharedArray</code>, of objects that are slow to create. I first create them and insert them in the array concurrently. After that, some readers concurrently access this <code>sharedArray</code>, get the objects in it and process it in their own way. I am experiencing crashes that I suppose come from not accessing the shared resource correctly. Am I doing it wrong?</p> <p>I am using ARC, with iOS target deployment version 5.1. I get the error <code>EXC_BAD_ACCESS</code> when setting the resources (marked in the code).</p> <p>Thank you very much! </p> <pre><code>//Creation of the array dispatch_queue_t constructionQueue = dispatch_queue_create("constructionQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_apply(numberOfSlowObjects, constructionQueue, ^(size_t i) { __block SlowObject *slowOb = [[SlowObject alloc] init]; dispatch_sync(dispatch_get_main_queue(), ^{ [sharedArray setObject:slowOb atIndexedSubscript:i]; //HERE I GET THE ERROR }); }); dispatch_release(constructionQueue); //Accessing the array from the different readers dispatch_queue_t readersQueue = dispatch_queue_create("readersQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_apply(readers.count, readersQueue, ^(size_t i) { Reader *reader = [readers objectAtIndex:i]; dispatch_queue_t processQueue = dispatch_queue_create("processQueue", DISPATCH_QUEUE_CONCURRENT); dispatch_apply(numberOfSlowObjects, processQueue, ^(size_t j) { __block SlowObject *slowOb; dispatch_sync(dispatch_get_main_queue(), ^{ slowOb = [sharedArray objectAtIndex:j]; }); [reader process:slowOb]; }); dispatch_release(processQueue); }); dispatch_release(readersQueue); </code></pre> <p>And the code of <code>sharedArray</code> (the shared resource) lazy initialization:</p> <pre><code>- (NSMutableArray *) sharedArray { if(!_sharedArray){ _sharedArray = [[NSMutableArray alloc] initWithCapacity:numberOfSlowObjects]; for(int i=0;i&lt;numberOfSlowObjects;i++) [_sharedArray addObject:[NSNumber numberWithInt:0]]; //null initialization; } return _sharedArray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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