[英]CFRunLoopWakeUp doesn't work?
I have a case where it seems that CFRunLoopWakeUp
isn't working. 我有一个案例,似乎CFRunLoopWakeUp
无法正常工作。 Here's the setup: 这是设置:
I have a "typical" while loop not on the main thread that waits for some work to complete: 我有一个“典型”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.");
}
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. 调用回调,但由于某种原因, CFRunLoopWakeUp
似乎没有做任何事情。 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: 我在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;
}
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:
. 切换到CFRunLoopRunInMode()
而不是runinMode:beforeDate:
. They're slightly different. 他们略有不同。 CFRunLoopStop()
rather than CFRunLoopWakeUp()
. 切换到CFRunLoopStop()
而不是CFRunLoopWakeUp()
。 And of course, make sure that self.runLoop
actually points to the runloop you think it does! 当然,确保self.runLoop
实际指向您认为它的runloop!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.