Skip to content

Instantly share code, notes, and snippets.

@danielctull
Created December 19, 2012 19:15
Show Gist options
  • Select an option

  • Save danielctull/4339601 to your computer and use it in GitHub Desktop.

Select an option

Save danielctull/4339601 to your computer and use it in GitHub Desktop.
Modified version of Steven Frank's example here: http://stevenf.com/notes/index.php/?Running+blocks+on+Grand+Central+Dispatch
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSArray *array = @[@"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine", @"ten", @"eleven", @"twelve"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_semaphore_t semaphore = dispatch_semaphore_create(6);
for (NSString *string in array) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(group, queue, ^{
NSLog(@"%@", string);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_group_t group2 = dispatch_group_create();
dispatch_group_enter(group2);
dispatch_group_notify(group, queue, ^{
NSLog(@"Group finished");
dispatch_release(semaphore);
dispatch_group_leave(group2);
});
dispatch_group_wait(group2, DISPATCH_TIME_FOREVER);
dispatch_release(group);
dispatch_release(group2);
NSLog(@"END");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment