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

how to get data in queue using GCD in ios

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

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

    }
}

这个循环中有大量数据,所以我想在 s=0 时获取,然后在 s=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)
}