Skip to content

Instantly share code, notes, and snippets.

@bcse bcse/main.m
Created Sep 27, 2017

Embed
What would you like to do?
[GCD] Use semaphore as lock
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
dispatch_semaphore_t lock = dispatch_semaphore_create(0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
NSLog(@"unlock");
dispatch_semaphore_signal(lock);
});
long timeout = dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
NSLog(@"done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.