Skip to content

Instantly share code, notes, and snippets.

@Leehro
Created October 30, 2013 19:18
Show Gist options
  • Save Leehro/7238482 to your computer and use it in GitHub Desktop.
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.
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);
});
}
@Leehro
Copy link
Author

Leehro commented Oct 30, 2013

If you want to allow 2 blocks in the queue at a time, create the semaphore with (2)!

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