首页 > 代码库 > Cocoa多线程编程之block与semaphore(信号量)
Cocoa多线程编程之block与semaphore(信号量)
首先大家要了解 dispatch_queue 的运作机制及线程同步
我们可以将许多 blocks 用 dispatch_async 函数提交到 dispatch_queue ,如果类型是DISPATCH_QUEUE_SERIAL (串行),那么这些 block 是按照 FIFO (先入先出)的规则调度的,也就是说,先加入的先执行,后加入的一定后执行,但在如果类型是DISPATCH_QUEUE_CONCURRENT(并行),那么某一时刻就可能有多个 block 同时在执行。
这个时候,如果两个 block 之间有依赖的话(比如说第二个 block 是基于第一个 block 的结果),这个时候我们就需要监听第一个 block 时候执行结束了。我们可以使用一些通信机制来解决这个问题,如:semaphore (信号量) 。
semaphore 的原理很简单,就是生产-消费模式,必须生产一些资源才能消费,没有资源的时候,我啥也不干,直到资源就绪(按照FIFO的规则等待资源就绪,一旦资源就绪并且得到调度了,我再执行)。
其中信号量主要的三个函数如下:
//创建一个信号量 sem,并将其资源初始值设置为0(不能少于0),一般表示任务还没有完成
1、dispatch_semaphore_t sem = dispatch_semaphore_create(0);
//增加 sem 计数(可理解为资源数),表明任务完成,有资源可用了
2、dispatch_semaphore_signal(sem);
//这就是减少 sem 的计数,如果资源少于0,则表明资源不可用
3、dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
示例:
sum = s;下面我们来看一个按照 FIFO 顺序执行并用 semaphore 同步的例子:先将数组求和再依次减去数组。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
initData();
__block int sum = 0;
// Create a semaphore with 0 resource
__block dispatch_semaphore_t sem = dispatch_semaphore_create(0);
__block dispatch_semaphore_t taskSem = dispatch_semaphore_create(0);
// create dispatch semaphore
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_block_t task1 = ^(void) {
int s = 0;
for (int i = 0; i < Length; i++)
s += data;
sum = s;
NSLog(@" >> after add: %d", sum);
dispatch_semaphore_signal(taskSem);
};
dispatch_block_t task2 = ^(void) {
dispatch_semaphore_wait(taskSem, DISPATCH_TIME_FOREVER);
int s = sum;
for (int i = 0; i < Length; i++)
s -= data;
sum = s;
NSLog(@" >> after subtract: %d", sum);
dispatch_semaphore_signal(sem);
};
dispatch_async(queue, task1);
dispatch_async(queue, task2);
// wait for the semaphore: wait until resource is ready.
//
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(sem);
dispatch_release(queue);
[pool drain];
sum = s;
NSLog(@" >> after add: %d", sum);
dispatch_semaphore_signal(taskSem);
};
dispatch_block_t task2 = ^(void) {
dispatch_semaphore_wait(taskSem, DISPATCH_TIME_FOREVER);
int s = sum;
for (int i = 0; i < Length; i++)
s -= data;
sum = s;
NSLog(@" >> after subtract: %d", sum);
dispatch_semaphore_signal(sem);
};
dispatch_async(queue, task1);
dispatch_async(queue, task2);
// wait for the semaphore: wait until resource is ready.
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(sem);
dispatch_release(queue);[pool drain];
sum = s;
NSLog(@" >> after subtract: %d", sum);
dispatch_semaphore_signal(sem);
};
dispatch_async(queue, task1);
dispatch_async(queue, task2);
// wait for the semaphore: wait until resource is ready.
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(sem);
dispatch_release(queue);
[pool drain];
NSLog(@" >> after add: %d", sum);
dispatch_semaphore_signal(taskSem);
};
dispatch_block_t task2 = ^(void) {
dispatch_semaphore_wait(taskSem, DISPATCH_TIME_FOREVER);
int s = sum;
for (int i = 0; i < Length; i++)
s -= data;
sum = s;
NSLog(@" >> after subtract: %d", sum);
dispatch_semaphore_signal(sem);
};
dispatch_async(queue, task1);
dispatch_async(queue, task2);
// wait for the semaphore: wait until resource is ready.
//
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(sem);
dispatch_release(queue);
[pool drain];
在上面的代码中,我们利用了 dispatch_queue 的 FIFO 特性,确保 task1 先于 task2 执行,而 task2 必须等待直到 task1 执行完毕才开始干正事,主线程又必须等待 task2 才能干正事。 这样我们就可以保证先求和,再相减,然后再让主线程运行结束这个顺序。
不对的地方请指教!