3

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!

kevlar
  • 1,110
  • 3
  • 17
  • 30

2 Answers2

0

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:

  • Switch to CFRunLoopRunInMode() rather than runinMode:beforeDate:. They're slightly different.
  • Switch to CFRunLoopStop() rather than CFRunLoopWakeUp().

And of course, make sure that self.runLoop actually points to the runloop you think it does!

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks for you suggestion Rob. I got it working by adding a CFRunLoopSource, calling CFRunLoopSourceSignal on that source, and then calling CFRunLoopWakeUp. – kevlar Jun 28 '12 at 05:39
-1

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

kevlar
  • 1,110
  • 3
  • 17
  • 30