Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Forked from Superbil/dispatch_semaphore.m
Created May 3, 2014 05:41
Show Gist options
  • Save Morse-Code/796eb9a437ae4474d88a to your computer and use it in GitHub Desktop.
Save Morse-Code/796eb9a437ae4474d88a to your computer and use it in GitHub Desktop.
dispatch_group_t group = dispatch_group_create();
dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int i = 0; i < 100; i++)
{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(group, queue, ^{
NSLog(@"%i",i);
sleep(2);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);
dispatch_release(semaphore);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment