I want to start many threads at a time, so I used the following code.
I am confused as to whether I should make a single queue and put many operations in it, or make many queues each with a single operation.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
for(int INDEX = 0; INDEX < 5; INDEX++) {
dispatch_async(queue, ^{
/*some code*/
});
}
for(int INDEX = 0; INDEX < 5; INDEX++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
/*some code*/
});
}
Which is the optimal one? Also, is there any limit on the number of threads in GCD?
You’re not making multiple queues. You’re retrieving one of the global queues. There are three of them in your app, and they can neither be created nor destroyed by you.
Additionally, even if you were creating queues (using
dispatch_queue_create()), you wouldn’t necessarily be creating a new thread per queue, or any new threads at all. The Grand Central Dispatch system manages all of the threads for you. GCD does not limit the number of queues you can create.Please have a read of the GCD reference and the Concurrency Programming Guide.