Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created August 28, 2014 01:08
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/c62e45554c2394bb6871 to your computer and use it in GitHub Desktop.
Save chriseidhof/c62e45554c2394bb6871 to your computer and use it in GitHub Desktop.
GCD Wrappers
import Foundation
// Executes an array of blocks in parallel, but only returns after they're all done.
func parallel(blocks: [() -> ()]) {
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for block in blocks {
dispatch_group_async(group, queue, block)
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
}
parallel(Array(1..<10).map { num in { print(num) } })
println("done")
@helje5
Copy link

helje5 commented Sep 2, 2014

You only need to 'import Dispatch', instead of the whole Foundation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment