前面有寫過一篇 .NET 使用WaitHandle開啟并發(fā)多線程查詢并同步返回
在.NET中有WaitHandle可以進(jìn)行多任務(wù)多線程的操作愚屁,作為成熟的語言O(shè)C中是否也有同樣的存在,于是我將目光轉(zhuǎn)向了蘋果杰作GCD
.
終于,我在官方的文檔中找到了 dispatch_block_wait
dispatch_group_wait
同步等待先前提交的塊對(duì)象完成; 如果在指定的超時(shí)時(shí)間結(jié)束之前塊沒有完成,則返回簇捍。
參數(shù)
- group
調(diào)度組等待盛撑。這個(gè)參數(shù)不能NULL。 - timeout
何時(shí)超時(shí)(請(qǐng)參閱dispatch_time)蟹瘾。的DISPATCH_TIME_NOW和DISPATCH_TIME_FOREVER常數(shù)提供方便圾浅。
可以看到以上需要兩個(gè)參數(shù):一是執(zhí)行等待任務(wù)的調(diào)度數(shù)組,二是等待超時(shí)的時(shí)間憾朴;
代碼
//任務(wù)1
dispatch_block_t t_block1 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
for (int i =0; i<10; i ++) {
printf("dispatch_block_create1\n");
}
});
//任務(wù)2
dispatch_block_t t_block2 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
for (int i =0; i<5; i ++) {
printf("dispatch_block_create2\n");
}
});
//任務(wù)3
dispatch_block_t t_block3 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
for (int i =0; i<30; i ++) {
printf("dispatch_block_create3\n");
}
});
//創(chuàng)建任務(wù)組
dispatch_group_t block_group = dispatch_group_create();
//創(chuàng)建隊(duì)列
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);
//向任務(wù)組中添加異步任務(wù)【將塊提交到調(diào)度隊(duì)列狸捕,并將塊與指定的調(diào)度組相關(guān)聯(lián)≈诶祝】
dispatch_group_async(block_group, aQueue, t_block1);
dispatch_group_async(block_group, aQueue, t_block2);
dispatch_group_async(block_group, aQueue, t_block3);
printf("BEGIN\n");
//執(zhí)行等待任務(wù)組灸拍,所有任務(wù)完成后返回(任務(wù)組,等待分派任務(wù)延遲時(shí)間)此處開啟多線程異步執(zhí)行多任務(wù)
dispatch_group_wait(block_group, dispatch_time(0, 10000000));
printf("END\n");
輸出結(jié)果
BEGIN
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
END
dispatch_group_wait 在所有的任務(wù)執(zhí)行完成后再返回,并且是多線程異步任務(wù)砾省,多任務(wù)操作的時(shí)間即可以大大縮小株搔,以上都是基于block的方式,蘋果也提供了另外一種添加調(diào)度隊(duì)列任務(wù)
- dispatch_group_async_f
將應(yīng)用程序定義的函數(shù)提交給調(diào)度隊(duì)列纯蛾,并將其與指定的調(diào)度組相關(guān)聯(lián)纤房。
void dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t queue, void *context, dispatch_function_t work);
參數(shù)
- group
將提交的功能與調(diào)度組關(guān)聯(lián)的調(diào)度組。該組由系統(tǒng)保留翻诉,直到應(yīng)用程序定義的函數(shù)運(yùn)行完成炮姨。這個(gè)參數(shù)不能NULL。 - queue
提交函數(shù)的調(diào)度隊(duì)列用于異步調(diào)用碰煌。隊(duì)列由系統(tǒng)保留舒岸,直到應(yīng)用程序定義的函數(shù)運(yùn)行完成。這個(gè)參數(shù)不能NULL芦圾。 - context
應(yīng)用程序定義的上下文參數(shù)傳遞給應(yīng)用程序定義的函數(shù)蛾派。 - work
應(yīng)用程序定義的函數(shù)在目標(biāo)隊(duì)列上調(diào)用。傳遞給該函數(shù)的第一個(gè)參數(shù)是context參數(shù)中的值个少。
function版本
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);
dispatch_group_t function_group = dispatch_group_create();
// 為結(jié)構(gòu)體分配內(nèi)存
gcd_f_data *context = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結(jié)構(gòu)體
if (context != NULL){
context->index = 0;
context->title = "GCD1";
// GCD執(zhí)行異步方法:指定主隊(duì)列(mainQueue)洪乍,傳遞結(jié)構(gòu)體數(shù)據(jù)(context)來執(zhí)行displayAlertView方法。
dispatch_group_async_f(function_group,aQueue,(void *)context, fun);
}
// 為結(jié)構(gòu)體分配內(nèi)存
gcd_f_data *context2 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結(jié)構(gòu)體
if (context2 != NULL){
context2->index = 1;
context2->title = "GCD2";
// GCD執(zhí)行異步方法:指定主隊(duì)列(mainQueue)夜焦,傳遞結(jié)構(gòu)體數(shù)據(jù)(context)來執(zhí)行displayAlertView方法壳澳。
dispatch_group_async_f(function_group,aQueue,(void *)context2, fun);
}
// 為結(jié)構(gòu)體分配內(nèi)存
gcd_f_data *context3 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結(jié)構(gòu)體
if (context3 != NULL){
context3->index = 2;
context3->title = "GCD3";
// GCD執(zhí)行異步方法:指定主隊(duì)列(mainQueue),傳遞結(jié)構(gòu)體數(shù)據(jù)(context)來執(zhí)行displayAlertView方法茫经。
dispatch_group_async_f(function_group,aQueue,(void *)context3, fun);
}
printf("BEGIN\n");
dispatch_group_wait(function_group, dispatch_time(0, 10000000));
printf("END\n");
//執(zhí)行任務(wù)的function
void fun(void * context)
{
gcd_f_data *data = (gcd_f_data *)context;
int indnx = 0;
if (data->index == 0) {
indnx = 10;
}else if (data->index == 1) {
indnx = 5;
}else if (data->index == 2) {
indnx = 25;
}
for (int i =0; i<indnx; i++) {
printf("%s\n",data->title);
}
}
//自定義數(shù)據(jù)結(jié)構(gòu)體
typedef struct{
int index;
char *title;
} gcd_f_data;
定義一個(gè)宏
/**
等待并發(fā)任務(wù)同步返回
@param array 任務(wù)數(shù)組(blocks)
@param timeout 延遲時(shí)間(納秒數(shù))
@return void
*/
#define waitQueueGroup(array,timeout) dispatch_group_t block_group = dispatch_group_create();dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);for (id item in array) {dispatch_block_t t_block1 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, item);dispatch_group_async(block_group, aQueue, t_block1);}dispatch_group_wait(block_group, dispatch_time(0, timeout));
使用示例
//創(chuàng)建任務(wù)數(shù)組
NSArray * array = @[^(void){
for (int i = 0; i< 10; i ++) {
printf("hello world 1\n");
}
},^(void){
for (int i = 0; i< 20; i ++) {
printf("hello world 2\n");
}
},^(void){
for (int i = 0; i< 5; i ++) {
printf("hello world 3\n");
}
}];
//調(diào)用
waitQueueGroup(array, 10000000);
END