如何在ios中使用GCD获取队列中的数据

时间:2017-04-26 10:03:07

标签: ios objective-c multithreading grand-central-dispatch

for (int s=0; s<masterArray.count; s++) {

       for (int i=0; i<countOfSub1; i++) {

    }
}

这个循环中的大量数据所以,我想在s = 0时得到然后得到第二个循环的所有数据然后在s = 1之后然后在得到第二个循环的所有数据之后,那么如何在此设置线程码。感谢。

1 个答案:

答案 0 :(得分:1)

您可以通过以下示例使用它

for (int s=0; s<masterArray.count; s++) {// your main loop starts here
dispatch_semaphore_t  sem;
sem = dispatch_semaphore_create(0);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{

for (int i=0; i<countOfSub1; i++) {// Inner loop in a thread
//your work here
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); // main loop waiting to be triggered from sub loop. (inner loop)
}