문제

Is this code safe to execute? That is, is it safe to add to a NSMutableArray or NSMutableDictionary concurrently?

- (NSArray *)batchProcess:(NSArray *)inputList {
    NSMutableArray *outputList = [NSMutableArray arrayWithCapacity:inputList.count];

    [inputList enumerateObjectsWithOptions:NSEnumerationConcurrent 
               usingBlock:^(id thing, NSUInteger index, BOOL *stop){
        id processedThing = [self doProcessingOn:thing];

        [outputList addObject:processedThing];
    }];

    return outputList;
}
도움이 되었습니까?

해결책

Is it safe to add to a NSMutableArray or NSMutableDictionary concurrently?

Unless you provide your own synchronization mechanism which guarantees only one thread uses it at any given time, It is NOT safe. If you use it in a way that is not thread safe, you should expect undefined behavior.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top