前面有寫過一篇 .NET 使用WaitHandle開啟并發(fā)多線程查詢并同步返回
在.NET中有WaitHandle可以進行多任務多線程的操作,作為成熟的語言OC中是否也有同樣的存在,于是我將目光轉向了蘋果杰作GCD.
終于,我在官方的文檔中找到了 dispatch_block_wait
dispatch_group_wait
同步等待先前提交的塊對象完成; 如果在指定的超時時間結束之前塊沒有完成,則返回。
參數(shù)
- group
調度組等待。這個參數(shù)不能NULL。 - timeout
何時超時(請參閱dispatch_time)。的DISPATCH_TIME_NOW和DISPATCH_TIME_FOREVER常數(shù)提供方便。
可以看到以上需要兩個參數(shù):一是執(zhí)行等待任務的調度數(shù)組,二是等待超時的時間;
代碼
//任務1
dispatch_block_t t_block1 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
for (int i =0; i<10; i ++) {
printf("dispatch_block_create1\n");
}
});
//任務2
dispatch_block_t t_block2 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
for (int i =0; i<5; i ++) {
printf("dispatch_block_create2\n");
}
});
//任務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)建任務組
dispatch_group_t block_group = dispatch_group_create();
//創(chuàng)建隊列
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);
//向任務組中添加異步任務【將塊提交到調度隊列,并將塊與指定的調度組相關聯(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í)行等待任務組,所有任務完成后返回(任務組,等待分派任務延遲時間)此處開啟多線程異步執(zhí)行多任務
dispatch_group_wait(block_group, dispatch_time(0, 10000000));
printf("END\n");
輸出結果
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 在所有的任務執(zhí)行完成后再返回,并且是多線程異步任務,多任務操作的時間即可以大大縮小,以上都是基于block的方式,蘋果也提供了另外一種添加調度隊列任務
- dispatch_group_async_f
將應用程序定義的函數(shù)提交給調度隊列,并將其與指定的調度組相關聯(lián)。
void dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t queue, void *context, dispatch_function_t work);
參數(shù)
- group
將提交的功能與調度組關聯(lián)的調度組。該組由系統(tǒng)保留,直到應用程序定義的函數(shù)運行完成。這個參數(shù)不能NULL。 - queue
提交函數(shù)的調度隊列用于異步調用。隊列由系統(tǒng)保留,直到應用程序定義的函數(shù)運行完成。這個參數(shù)不能NULL。 - context
應用程序定義的上下文參數(shù)傳遞給應用程序定義的函數(shù)。 - work
應用程序定義的函數(shù)在目標隊列上調用。傳遞給該函數(shù)的第一個參數(shù)是context參數(shù)中的值。
function版本
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);
dispatch_group_t function_group = dispatch_group_create();
// 為結構體分配內存
gcd_f_data *context = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結構體
if (context != NULL){
context->index = 0;
context->title = "GCD1";
// GCD執(zhí)行異步方法:指定主隊列(mainQueue),傳遞結構體數(shù)據(jù)(context)來執(zhí)行displayAlertView方法。
dispatch_group_async_f(function_group,aQueue,(void *)context, fun);
}
// 為結構體分配內存
gcd_f_data *context2 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結構體
if (context2 != NULL){
context2->index = 1;
context2->title = "GCD2";
// GCD執(zhí)行異步方法:指定主隊列(mainQueue),傳遞結構體數(shù)據(jù)(context)來執(zhí)行displayAlertView方法。
dispatch_group_async_f(function_group,aQueue,(void *)context2, fun);
}
// 為結構體分配內存
gcd_f_data *context3 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
// 初始化結構體
if (context3 != NULL){
context3->index = 2;
context3->title = "GCD3";
// GCD執(zhí)行異步方法:指定主隊列(mainQueue),傳遞結構體數(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í)行任務的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ù)結構體
typedef struct{
int index;
char *title;
} gcd_f_data;
定義一個宏
/**
等待并發(fā)任務同步返回
@param array 任務數(shù)組(blocks)
@param timeout 延遲時間(納秒數(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)建任務數(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");
}
}];
//調用
waitQueueGroup(array, 10000000);
END