NSMutableArray集合和@Synchronized块 [英] NSMutableArray collection and @Synchronized blocks

查看:110
本文介绍了NSMutableArray集合和@Synchronized块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective C中,我正在使用来自各种线程的NSMutableArray实例,并且我正在使用@synchronized来使其线程安全。目前我对这个数组的所有访问都受到@synchronized块的保护,甚至是objectAtIndex:方法。不过我想知道哪些方法确实需要用@synchronized来保护。我需要保护读访问权吗?



如果'ObjectAtIndex'没有受到保护并且在'removeObject'的同时被调用,会发生什么?



如果所有方法都受@synchronized保护,性能如何? (我正在编写一个tcp / udp游戏服务器,如果它会减少性能或生成锁定,我真的不想过度保护这些数组)。



例如我假设'containsObject:'方法将枚举以查找对象,并且我应该避免在另一个线程中对'removeObject:'进行concurent调用。 / p>

也许一个好的解决方案是拥有太多不同的锁(用于读写访问)......



欢迎提供帮助和建议!



非常感谢。



请在下面找到示例代码来说明:

  @interface TestClass:NSObject 
{
NSMutableArray * array;
}

@end

@implementation TestClass

- (id)init
{
self = [super init];
if(self)
{
array = [NSMutableArray array];
}
返回自我;
}

- (id)objectAtIndex:(NSUInteger)index
{
@synchronized(array)** //它是否有用?**
{
return [array objectAtIndex:index];
}
}

- (void)removeObject:(id)object
{
@synchronized(array)
{
[array removeObject:object];
}
}

- (无效)计算
{
@synchronized(数组)
{
for(id object)在数组中)
{
[object compute];
}
}
}

@end


解决方案

是的,您必须同步读取访问以防止它们与突变同时发生。但是,读取访问可以安全地与其他读取访问同时运行。



如果您有多个读取器,则值得研究读写锁定方案。你可以使用pthread读写锁(即 pthread_rwlock _...())。



或者,你可以在OS X 10.7+和iOS 5+上使用GCD和屏障例程。创建专用并发队列。以正常方式向其提交所有读取操作(例如 dispatch_sync())。使用屏障例程(例如 dispatch_barrier_async())向其提交变异操作。 (它可以是异步的,因为在继续之前你通常不需要知道变异已经完成。你只需要知道在提交变异后提交的所有读数都会看到变异的结果,并且障碍保证了这一点。 )



如果您有权访问 WWDC 2011会话视频,用于会议210 - 掌握Grand Central Dispatch


In Objective C I'm using a NSMutableArray instance from various thread and I'm using @synchronized to make it thread safe. currently all my acces to this array are protected with a @synchronized block, even objectAtIndex: method. Nevertheless I wonder which methods call really need to be protected with @synchronized. do I need to protect read access ?

What would happens if 'ObjectAtIndex' is not protected and called at the same time that 'removeObject' ?

If all methods are protected by a @synchronized what about performance? (I'writing a tcp/udp game server and really don't want to overprotect these array if it would decrease perf or generate locks).

for example I suppose that the 'containsObject:' method will enumerate to find the object and that I should avoid a concurent call to 'removeObject:' in another thread.

Perhaps a good solution would be to have too different locks (for read and write access)...

Help and suggestion are welcome !

Thanks a lot.

Please find a sample code below to illustrate :

@interface TestClass : NSObject
{
    NSMutableArray * array;
}

@end

@implementation TestClass

- (id)init
{
    self = [super init];
    if (self)
    {
        array = [NSMutableArray array];
    }
    return self;
}

-(id)objectAtIndex:(NSUInteger)index
{
    @synchronized(array) **// IS IT USEFUL OR NOT ??**
    {
        return [array objectAtIndex:index];
    }
}

-(void)removeObject:(id)object
{
    @synchronized(array)
    {
        [array removeObject:object];
    }
}

-(void)compute
{
    @synchronized(array)
    {
        for (id object in array)
        {
            [object compute];
        }
    }
}

@end

解决方案

Yes, you have to synchronize read accesses to prevent them from happening simultaneously with mutations. Read accesses can safely run simultaneously with other read accesses, though.

If you have multiple readers, then it's worth investigating a read-write locking scheme. You can use pthread read-write locks (i.e. pthread_rwlock_...()).

Alternatively, you can use GCD on OS X 10.7+ and iOS 5+ with the "barrier" routines. Create a private concurrent queue. Submit all read operations to it in the normal fashion (e.g. dispatch_sync()). Submit mutation operations to it using a barrier routine such as dispatch_barrier_async(). (It can be asynchronous because you generally don't need to know that the mutation has completed before continuing. You only need to know that all reads submitted after you submit the mutation will see the results of the mutation, and the barrier guarantees that.)

You can learn more about this if you have access to the WWDC 2011 session video for Session 210 - Mastering Grand Central Dispatch.

这篇关于NSMutableArray集合和@Synchronized块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆