Created
October 30, 2013 19:18
-
-
Save Leehro/7238482 to your computer and use it in GitHub Desktop.
I love this. Use a dispatch_semaphore_t to limit how many blocks you dispatch to a queue.
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
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1); | |
// Then in some loop ... | |
if(dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW) == 0) { | |
dispatch_async(queue, ^{ | |
// Some code that needs to run but doesn't need to fill up the queue. | |
// | |
dispatch_semaphore_signal(semaphore); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to allow 2 blocks in the queue at a time, create the semaphore with (2)!