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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句