-
-
Save khanlou/c5664183cfa5505a4eb011656c2c0e46 to your computer and use it in GitHub Desktop.
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
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