我有一个案例似乎CFRunLoopWakeUp
无效。这是设置:
我在主线程上有一个“典型”while循环不,等待完成一些工作:
- (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.");
}
我有一个回调函数,等待一些工作完成后。这也是从主线程调用而不是:
- (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!
}
调用回调,但出于某种原因,CFRunLoopWakeUp
似乎没有做任何事情。我错过了一些明显的东西吗这里有一些深层次的线程问题吗?谢谢!
答案 0 :(得分:0)
我能够通过添加源代码来使CFRunLoopWakeUp工作,因为这个人解释说:http://www.cocoabuilder.com/archive/cocoa/112261-cfrunlooptimer-firing-delay.html
答案 1 :(得分:0)
首先,我无法重现您的问题。我在这样的GCD中构建它:
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;
}
你能建立一个更简单的程序来证明这个问题吗?
我会尝试的其他事情,主要是为了调试目的来缩小问题范围:
CFRunLoopRunInMode()
而不是runinMode:beforeDate:
。它们略有不同。CFRunLoopStop()
而不是CFRunLoopWakeUp()
。当然,确保self.runLoop
实际指向您认为它的runloop!