Last active
October 13, 2019 03:30
-
-
Save chrismsimpson/bc397bbbb2d161884069738ddccdbc99 to your computer and use it in GitHub Desktop.
Parallel extensions for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Parallel.swift | |
// | |
import Dispatch | |
extension DispatchQueue { | |
public func parallel(_ blocks: Array<() -> Void>) { | |
self.parallel(blocks.map { (block) in DispatchWorkItem(block: block) }) | |
} | |
public func parallel(_ blocks: Array<DispatchWorkItem>) { | |
let group = DispatchGroup() | |
blocks.forEach { [weak self, weak group] (block) in | |
guard let group = group else { | |
return | |
} | |
self?.async(group: group, execute: block) | |
} | |
_ = group.wait(timeout: .distantFuture) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment