Last active
December 31, 2018 08:43
-
-
Save prachigauriar/8118909 to your computer and use it in GitHub Desktop.
An example of using -[NSCondition waitUntilDate:].
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
// | |
// NSCondition-WaitUntilDate-Example.m | |
// NSConditionExample | |
// | |
// Created by Prachi Gauriar on 12/23/2013. | |
// Copyright (c) 2013 Prachi Gauriar. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) | |
{ | |
const NSTimeInterval QLCTimeoutInterval = 4.0; | |
@autoreleasepool { | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_group_t group = dispatch_group_create(); | |
NSCondition *condition = [[NSCondition alloc] init]; | |
__block BOOL predicate = NO; | |
// Block that sets the predicate and then signals | |
dispatch_group_async(group, queue, ^{ | |
// Sleep time is between 1 and 2*QLCTimeInterval seconds | |
uint32_t sleepTimeInterval = arc4random_uniform(2 * QLCTimeoutInterval) + 1; | |
NSLog(@"Sleeping for %u seconds.", sleepTimeInterval); | |
sleep(sleepTimeInterval); | |
[condition lock]; | |
NSLog(@"Signaling."); | |
predicate = YES; | |
[condition signal]; | |
[condition unlock]; | |
}); | |
// Block that waits for the predicate to be set | |
NSDate *startDate = [NSDate date]; | |
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:QLCTimeoutInterval]; | |
dispatch_group_async(group, queue, ^{ | |
NSLog(@"Waiting for signal."); | |
BOOL signaled = NO; | |
[condition lock]; | |
// Keep waiting until either the predicate is true or we timed out | |
while (!predicate && (signaled = [condition waitUntilDate:timeoutDate])) { | |
} | |
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate]; | |
[condition unlock]; | |
NSLog(@"%@ after %.2f seconds.", signaled ? @"Signaled" : @"Timed out", elapsedTime); | |
}); | |
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment