Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created June 9, 2016 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanlou/c5664183cfa5505a4eb011656c2c0e46 to your computer and use it in GitHub Desktop.
Save khanlou/c5664183cfa5505a4eb011656c2c0e46 to your computer and use it in GitHub Desktop.
class LimitedWorker {
private let concurrentQueue = dispatch_queue_create("com.khanlou.concurrent.queue", DISPATCH_QUEUE_CONCURRENT)
private let semaphore: dispatch_semaphore_t
init(limit: Int) {
semaphore = dispatch_semaphore_create(limit)
}
func enqueueWork(work: () -> ()) {
dispatch_async(concurrentQueue) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
work()
dispatch_semaphore_signal(semaphore)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment