4

I have a dedicated networking thread in my iOS app. The thread's -main method looks like this:

- (void)main
{   
    @try 
    {       
        while (!self.isCancelled) 
        {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

            [[NSRunLoop currentRunLoop] run];

            [pool drain];
        }
    }
    @catch (NSException * e) 
    {
        NSLog(@"%@", e);
    }
}

I have an NSOperationQueue populated with http operations (concurrent, run-loop-based NSOperation subclass). Now since I know when there are http operations in need of the runloop thread, I would like to be able to prevent the runloop thread from executing when there is no work to do. I read somewhere (helpful, I know) that NSRunLoop automatically sleeps when it has no work to do. However, I used Instruments' time profiler and saw that the runloop thread was always active.

How can I arrange my use of NSRunLoop and NSThread to prevent this waste of resources?

LinenIsGreat
  • 594
  • 4
  • 13
  • Please edit your question to include the stack trace showing the thread being “active”. (And note that Instruments shows the stacks of all threads by default.) – Peter Hosey Mar 20 '11 at 01:03
  • To clarify: All threads show up in Instruments, even if they're sleeping. If a thread is asleep, for instance blocked in a call to mach_msg_trap, it will show up in instruments with all the samples being on that frame. Doesn't mean it's consuming resources. – ipmcc Dec 22 '12 at 00:24

0 Answers0