NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 編程
// 建立一個串行分發隊列 數組
dispatch_queue_t queue = dispatch_queue_create("studyBlocks", NULL); session
// 將一個 block 任務加入到其中並行運行. 這樣 block 就會在新的線程中運行,直到結束返回主線程 併發
// 加入 dispatch_queue 中的 block 必須是無參數也無返回值的 app
dispatch_async(queue, ^(void){ async
int sum = 0; 函數
for (int i = 0; i<100; i++) { this
sum += i; spa
} 線程
NSLog(@"sum:%d",sum);
});
dispatch_release(queue);
[pool drain];
一、dispatch_queue_t 類型 的定義以下:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[[self captureManager] session] startRunning];
});
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
initData();
// Create a semaphore with 0 resource
__block dispatch_semaphore_t sem = dispatch_semaphore_create(0);
// create dispatch semaphore
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL); dispatch_async(queue, ^(void) {int sum = 0;
for(int i = 0; i < Length; i++)
sum += data;
NSLog(@" >> Sum: %d", sum);// signal the semaphore: add 1 resource
dispatch_semaphore_signal(sem);});
// wait for the semaphore: wait until resource is ready.
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_release(sem);
dispatch_release(queue);
[pool drain];
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];
在上面的代碼中,咱們利用了 dispatch_queue 的 FIFO 特性,確保 task1 先於 task2 執行,而 task2 必須等待直到 task1 執行完畢纔開始幹正事,主線程又必須等待 task2 才能幹正事。 這樣咱們就能夠保證先求和,再相減,而後再讓主線程運行結束這個順序。
使用 dispatch_apply 進行併發迭代:
對於上面的求和操做,咱們也可使用 dispatch_apply 來簡化代碼的編寫.
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
initData();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block int sum = 0;
__block int *pArray = data;
// iterations
//
dispatch_apply(Length, queue, ^(size_t i) {
sum += pArray;
});
NSLog(@" >> sum: %d", sum);
dispatch_release(queue);
[pool drain];
dispatch group
咱們能夠將完成一組相關任務的 block 添加到一個 dispatch group 中去,這樣能夠在 group 中全部 block 任務都完成以後,再作其餘事情。好比 6 中的示例也可使用 dispatch group 實現:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
initData();
__block int sum = 0;
// Create a semaphore with 0 resource
//
__block dispatch_semaphore_t taskSem = dispatch_semaphore_create(0);
// create dispatch semaphore
//
dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);
dispatch_group_t group = dispatch_group_create();
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);
};
// Fork
dispatch_group_async(group, queue, task1);
dispatch_group_async(group, queue, task2);
// Join
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(taskSem);
dispatch_release(queue);
dispatch_release(group);
[pool drain];
在上面的代碼中,咱們使用 dispatch_group_create 建立一個 dispatch_group_t,而後使用語句:dispatch_group_async(group, queue, task1)將 block 任務加入隊列中,並與組關聯,這樣咱們就可使用 dispatch_group_wait(group, DISPATCH_TIME_FOREVER)來等待組中全部的 block 任務完成再繼續執行。
至此咱們瞭解了 dispatch queue 以及 block 並行編程相關基本知識,開始在項目中運用它們吧。