Created
January 23, 2013 16:04
-
-
Save slightair/4608690 to your computer and use it in GitHub Desktop.
dispatch_semaphore test?
This file contains hidden or 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
#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