Skip to content

Instantly share code, notes, and snippets.

@slightair
Created January 23, 2013 16:04
Show Gist options
  • Save slightair/4608690 to your computer and use it in GitHub Desktop.
Save slightair/4608690 to your computer and use it in GitHub Desktop.
dispatch_semaphore test?
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
int main (int argc, char const *argv[])
{
int i;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
for(i = 0; i < 10; i++) {
dispatch_async(queue, ^{
NSLog(@"signal %d %@", i, [NSThread currentThread]);
dispatch_semaphore_signal(semaphore);
});
}
dispatch_async(queue, ^{
NSLog(@"wait %@", [NSThread currentThread]);
int j = 0;
while(1) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"count: %d", j);
sleep(1);
j++;
}
});
dispatch_main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment