Search code examples
iosmultithreadingcfrunloop

CFRunLoopWakeUp doesn't work?


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!


Solution

  • I was able to make CFRunLoopWakeUp work by adding a source as this guy explains: http://www.cocoabuilder.com/archive/cocoa/112261-cfrunlooptimer-firing-delay.html