多线程的数据同步
多线程会导致数据竞争,和数据冲突,当发生多线程数据冲突的时候错误将会非常难于检查,有的时候甚至需要彻底检查一遍代码才会发现。
尽量避免线程间数据共享。如果必须要交互那么就要使用同步锁来确保数据安全性。
ios 提供了很多同步工具。如下
原子操作
原子操作并不会想线程锁那样去阻断线程,所以性能会高很多。
内存屏障 Memory Barrier
mb(memory barrier)可以让cpu或编译器强制将barrier之前和之后的内存操作分开。
locks
简而言之,lock会让某一块代码和资源在同一时刻只能被一个线程访问。然而枷锁也有可能会影响你代码的正确性。
- posix mutex lock
pthread_mutex_t mutex;
void MyInitFunction()
{
pthread_mutex_init(&mutex, NULL);
}
void MyLockingFunction()
{
pthread_mutex_lock(&mutex);// 枷锁
// 业务代码
pthread_mutex_unlock(&mutex);// 解锁
}
- NSLock
BOOL moreToDo = YES;
NSLock *theLock = [[NSLock alloc] init];
...
while (moreToDo) {
/* Do another increment of calculation */
/* until there’s no more to do. */
if ([theLock tryLock]) {
/* Update display used by all threads. */
[theLock unlock];
}
}
- @synchronized
使用简单,不必创建lock等对象。例如
- (void)myMethod:(id)anObj
{
@synchronized(anObj)
{
// Everything between the braces is protected by the @synchronized directive.
}
}
注意,如果在不同的线程里操作了不同的aOBj就不会阻断任何一方的线程,而如果两个线程使用了同一个aObj那么就会发生阻断。
使用synchronized必须允许 exception handling 开启。
conditions 条件执行
根据条件进行执行
流程:
- 锁定对象
- 检查bool型的判断条件
- 如果2步骤中bool为false ,那么就执行对象的wati 和 waitUntilDate。指导bool检查为true为止,
- 如果bool为true,执行任务。
- 修改bool
- task结束,解锁对象。
伪代码:
lock the condition
while (!(boolean_predicate)) {
wait on condition
}
do protected work
(optionally, signal or broadcast the condition again or change a predicate value)
unlock the condition
执行selector的规律
当给一个对象异步分发消息的时候,消息到达另外的线程会加入到目的线程的runloop中排队执行,FIFO。Cocoa Perform Selector Sources
线程锁的消耗
线程锁和原子操作导致大量的系统资源消耗,线程锁会使用memoryBarrier,和内核同步机制确保代码的正确性,但是如果有死锁或者其他问题,那么,多线程的体验可能还不如单线程。
信号处理
多线程信号处理比较复杂,可能会跨线程 signal
线程安全指南
尽量避免线程锁
尽量让某一个线程使用独立的数据,这样就可以避免数据共享导致的问题。
获取相同的mutex
保证代码正确和有效性
比如
NSLock* arrayLock = GetArrayLock();
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
[anObject doSomething];
[arrayLock unlock];
NSLock* arrayLock = GetArrayLock();
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
[anObject retain];
[arrayLock unlock];
[anObject doSomething];
[anObject release];
上面的代码是没有错误的,但是如果[anObject doSomething];
执行时间较长,那么就会影响效率。
如果采用下面的方法,先retain,然后在release既保证了对象有效性,又解决了效率问题。
活锁和死锁
避免活锁和死锁的最好方法就是在同一时间只用一个锁。
慎用Volatile关键字
volatile关键字会保证每次读取变量都是从内存中读取,但并不一定是正确的。
原子操作数据
原子操作效率跟syn效率差不多
int32_t theValue = 0;
OSAtomicTestAndSet(0, &theValue);
// theValue is now 128.
theValue = 0;
OSAtomicTestAndSet(7, &theValue);
// theValue is now 1.