[ios]NSLock锁
线程同步:
NSLock* arrayLock = [self GetArrayLock];
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
[arrayLock unlock];
// 在处理doSomething的时候共享资源myArray可能被修改,下面的操作就是有风险的操作
[anObject doSomething];
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
// 在处理doSomething放到Lock里面,如果doSomething处理时间比较长,那么就形成了效率瓶颈,影响程序效率
[anObject doSomething];
[arrayLock unlock];
NSLock* arrayLock = [self GetArrayLock];
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
// 把对象retain and save,防止在unlock,myArray里面的内容被修改
[anObject retain];
[arrayLock unlock];
[anObject doSomething];
[anObject release];