gpt4 book ai didi

ios - CFRunLoopWakeUp 不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:00 30 4
gpt4 key购买 nike

在我的案例中,CFRunLoopWakeUp 似乎无法正常工作。这是设置:

我在等待某些工作完成的主线程上有一个“典型的”while 循环not:

- (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 似乎没有做任何事情。我错过了一些明显的东西吗?这里是否存在一些深度线程问题?谢谢!

最佳答案

首先,我无法重现您的问题。我像这样在 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 实际上指向您认为它指向的运行循环!

关于ios - CFRunLoopWakeUp 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205654/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com