线程安全:NSOperationQueue + [array addObject]

时间:2011-12-21 23:45:40

标签: iphone ios concurrency nsoperationqueue

我找不到任何使用操作队列时如何处理相同(类)变量的示例。在C&它涉及互斥体。那么,当NSOperationQueue启动一个线程进行操作并且类变量被修改时会发生什么?它是线程安全的吗?谢谢。

@interface MyTest {
    NSMutableArray *_array;
}
@end

-(id)init
{
    ...
    _array = [NSMutableArray new]; // class variable

        // queue time consuming loading
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation =
        [NSInvocationOperation initWithTarget:self
                                     selector:@selector(populate)
                                       object:nil];
    [queue addOperation:operation];

        // start continuous processing
    [NSTimer scheduledTimerWithTimeInterval:0.1
                                     target:self
                                   selector:@selector(processing)
                                   userInfo:nil
                                    repeats:YES];
    ...
}

-(void)populate
{
    while (...)
    {
        id element = ...; // time consuming

            // modify class variable "_array" from operation's thread (?)
        [_array addObject:element];

            // Ok, I can do instead of addObject
            // performSelectorOnMainThread:withObject:waitUntilDone:
            // but is it the only way? Is it needed?
    }
}

    // access and/or modify class variable "_array"
-(void)processing
{
    NSLog(@"array.count = %d", array.count);
    for (id i in _array)
    {
        [_array addObject:[NSNumber numberWithInt:rand() % 100]];
            // etc...
    }
}

1 个答案:

答案 0 :(得分:1)

不,这不是线程安全的,如果你启动一个线程对一个类变量做一些工作,可以被其他线程修改然后它不是线程安全的,如果在运行populate时从某个线程调用处理另外,当foreach循环看到数组已被修改时,你可能会得到一个异常,尽管你会在你的示例中修改foreach循环内的数组时得到该异常(你不应该这样做,程序会抛出一个例外)...解决这个问题的一种方法是使用数组上的同步块,它将确保同步块不会同时执行,线程阻塞直到一个同步块结束,例如

    -(void)populate
    {


        while (...)
        {
            id element = ...; // time consuming

                // modify class variable "_array" from operation's thread (?)
      @synchronized(_array)
        {
            [_array addObject:element];

        }          // Ok, I can do instead of addObject
                // performSelectorOnMainThread:withObject:waitUntilDone:
                // but is it the only way? Is it needed?
        }

    }

        // access and/or modify class variable "_array"
    -(void)processing
    {


          @synchronized(_array)
         {
            NSLog(@"array.count = %d", array.count);
            for (id i in _array)
            {
                //you shouldnt modify the _array here you will get an exception
                    // etc...
            }
        }
    }