Created
September 27, 2017 03:56
-
-
Save bcse/bb8a23559b2b2b3ff570006ba7783dd4 to your computer and use it in GitHub Desktop.
[GCD] Use semaphore as lock
This file contains 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> | |
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