views:

56

answers:

2

I need to guarantee that the same thread performs various actions at arbitrary times. First the thread needs to initialize a library, then I want the thread to sleep until work needs to be done and upon user input, I need to be able to pass selectors or blocks for execution.

How can I setup an NSRunLoop to sleep after initialization? After which, how do I signal the run loop to wake up and do something?

I've tried reading the Threading Programming Guide for iOS, but I'd like to avoid setting up classes as custom input classes and use something more lightweight like performSelector:onThread:

Can I set a timer to fire forever from now so the run loop doesn't end?

Here's essentially what I want in pseudo-code:

// Initialization Code...

do {
    sleepUntilSignaled();
    doWorkSentToThisThread();
while (!done);

Where I send the work to do as a performSelector:onThread: message. It would be even better if I could send the run loop a block like: ^{[someObj message]; [otherObj otherMsg];} but I'd be happy with performSelector since I'm pretty sure that's possible without much extra coding.

Thanks!

A: 

I think you can use NSInvocationOperation with NSOperationQueue.

Toro
Operation queues do not reuse the same thread if the time between invocations is too long. Thanks for the suggestion though.
Ben S
+2  A: 

You have all the necessary pieces together in your question. You start your thread and have it run it’s runloop. If you need the thread to do something you can use performSelector:onThread: on the main thread to do it.

There is one thing with the runloop you have to be aware though: It won’t run unless it has an input source or a timer attached to it. Just attach a timer to the run loop that fires some time in the distant future and you’re all set.

// Initialization code here

[NSTimer scheduledTimerWithTimeInterval: FLT_MAX
                                 target: self selector: @selector(doNothing:)
                               userInfo: nil repeats:YES];

NSRunLoop *rl = [NSRunLoop currentRunLoop];
do {
    [rl runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);

Using performSelector:onThread:withObject: you can also pass your block to the background thread. All you need to do is to write a method somewhere that takes an block as a parameter and runs it:

@interface NSThread (sendBlockToBackground)
- (void) performBlock: (void (^)())block;
@end

@implementation NSThread (sendBlockToBackground)
- (void) performBlock: (void (^)())block;
{
    [self performSelector: @selector(runBlock:) 
                 onThread: self withObject: block waitUntilDone: NO];
}

- (void) runBlock: (void (^)())block;
{
    block();
}
@end

But maybe you should use a dispatch queue instead of all this. This requires less code and probably has less overhead also:

dispatch_queue_t myQueue = dispatch_queue_create( "net.example.product.queue", NULL );
dispatch_async( myQueue, ^{
    // Initialization code here
} );

// Submit block:
dispatch_async( myQueue, ^{
    [someObject someMethod: someParameter];
} );

A dispatch queue created using dispatch_queue_create is a serial queue - all blocks sent to it will be performed in the same order they arrived, one after another.

Sven
Thanks for the code samples! I was originally using a dispatch queue but it doesn't reuse the same thread if there's too much time between dispatches.
Ben S
Yes, this is true. But it doesn’t really matter, unless your library code depends on which thread it is run in. This usually is not the case. Even if your library says that it is not thread-safe this does not matter, as long as it isn’t called from more than one thread *at the same time*. And this will never happen with a serial queue.
Sven
The library I am using is PJSUA and when it fails assertions if it detects that a calling thread is different than the initializing thread :(
Ben S