How about something like this:
func runThingsInTheBackground() { var semaphores = [dispatch_semaphore_t]() for delay in [2, 3, 10, 7] { var semaphore = dispatch_semaphore_create(0) semaphores.append(semaphore) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { sleep(UInt32(delay)) println("Task took \(delay) seconds") dispatch_semaphore_signal(semaphore) } } for semaphore in semaphores { dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) } }
This is very similar to what you have. My work "queue" is an array of seconds to sleep, so you can see that everything is happening in the background.
Note that this just runs all the tasks in the background. If you want to limit the number of active tasks, for example, the number of CPU cores, you need to do a little more work.
Not sure if this is what you were looking for, let me know.
source share