method1:
- (void) method1
{
[_condition lock];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
//Fetach data from remote, when finished call method2
[self fetchData];
});
[_condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:30.0]];
// Do something.
[_condition unlock];
}
method2:
- (void) method2
{
[_condition lock];
[_condition signal];
[_condition unlock];
}
If Thread 1 is in method1, by executing [_condition waitUtilDate ...];
it unlocks its lock. Thread 2 entered this area and also wait on the condition by executing [_condition waitUtilDate ...]
.
Both Thread 1 and Thread 2 enqueued a Block(request 1 request 2) to fetch the same data from remote. When request 1 finishes, it calls method2 to signal _condition:
My questions are:
- Which will be signaled, Thread 1 or Thread 2 ?
- 'cause request 1 and request 2 are doing the same thing, I can signal both threads(
broadcast
) and cancel request 2 when request 1 finishes. But, a better way is to refuse Thread 2 to enter the critical area, after request 1 is sent out. But I coundn't lock twice before entering the critical area. So what can I do?
Thanks.