Skip to content

Instantly share code, notes, and snippets.

@bcse
Created September 27, 2017 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcse/bb8a23559b2b2b3ff570006ba7783dd4 to your computer and use it in GitHub Desktop.
Save bcse/bb8a23559b2b2b3ff570006ba7783dd4 to your computer and use it in GitHub Desktop.
[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