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

时间:2021-12-12 00:08:42
for (int s=0; s<masterArray.count; s++) {

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

    }
}

vast amount of data in this loop so, i want get when s=0 then get all data of second loop then after s=1 then after get all data of second loop so on, then how can i set thread in this code. Thanks.

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

1 个解决方案

#1


1  

You can use it by the following example

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

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)
}

#1


1  

You can use it by the following example

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

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)
}