I have a case where it seems that CFRunLoopWakeUp
isn't working. Here's the setup:
I have a "typical" while loop not on the main thread that waits for some work to complete:
- (void)someFunc
{
self.runLoop = CFRunLoopGetCurrent();
NSLog(@"Pre loop.");
while (!self.completed)
{
NSLog(@"In loop.");
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[pool release];
}
NSLog(@"Post loop.");
}
I have a callback function that waits for when some work is done. This is also not called from the main thread:
- (void)callback
{
NSLog(@"Work completed.");
self.completed = YES;
// I've checked that CFRunLoopIsWaiting(self.runLoop) here returns true
CFRunLoopWakeUp(self.runLoop); // Should wake up the waiting run loop, but doesn't!
}
The callback is called, but for some reason, CFRunLoopWakeUp
doesn't seem to do anything. Am I missing something obvious? Are there some deep threading issues going on here? Thanks!
我能够通过添加源代码来使CFRunLoopWakeUp工作,因为这个人解释说: http : //www.cocoabuilder.com/archive/cocoa/112261-cfrunlooptimer-firing-delay.html
First, I can't reproduce your problem. I'm building it in GCD like this:
int main (int argc, const char *argv[])
{
@autoreleasepool {
__block BOOL completed = NO;
__block CFRunLoopRef runLoop;
dispatch_queue_t queue1 = dispatch_queue_create("first", 0);
dispatch_queue_t queue2 = dispatch_queue_create("second", 0);
dispatch_async(queue1, ^{
runLoop = CFRunLoopGetCurrent();
NSLog(@"Pre loop.");
while (!completed)
{
NSLog(@"In loop.");
@autoreleasepool {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
NSLog(@"Post loop.");
});
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (unsigned)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, queue2, ^(void) {
NSLog(@"Work completed.");
completed = YES;
// I've checked that CFRunLoopIsWaiting(self.runLoop) here returns true
CFRunLoopWakeUp(runLoop); // Should wake up the waiting run loop, but doesn't!
});
dispatch_sync(queue1, ^{});
dispatch_sync(queue2, ^{});
dispatch_release(queue1);
dispatch_release(queue2);
}
return 0;
}
Can you build a simpler program that demonstrates the problem?
Other things I would try, mostly for debugging purposes to narrow the issue:
CFRunLoopRunInMode()
rather than runinMode:beforeDate:
. They're slightly different. CFRunLoopStop()
rather than CFRunLoopWakeUp()
. And of course, make sure that self.runLoop
actually points to the runloop you think it does!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.