Re: Can't get timer to fire on separate thread
Re: Can't get timer to fire on separate thread
- Subject: Re: Can't get timer to fire on separate thread
- From: Rick Mann <email@hidden>
- Date: Tue, 21 Oct 2014 01:58:51 -0700
> On Oct 21, 2014, at 01:46 , Ken Thomases <email@hidden> wrote:
>
> On Oct 21, 2014, at 3:15 AM, Rick Mann <email@hidden> wrote:
>
>> I'm using CocoaHTTPServer and RoutingHTTPServer, and when a request comes in, my handler block is called on some arbitrary thread. The run loop associated with it has kCFRunLoopDefaultMode.
>>
>> I have code that creates an NSTimer and invokes a block passed to it. This works fine when called from the main thread, but doesn't work when called in the HTTP server handler. So, I tried installing the timer on the current run loop with first common modes and then default mode. In neither case does it fire.
>>
>> Any idea what I might be doing wrong?
>
> Arbitrary threads can't be relied upon to run their run loops. Only the main thread and threads which you control (and for which you've written code to explicitly run the run loop) can be relied on to do that.
>
> What are you trying to achieve? What is the timer for?
>
> Is there a reason you can't shunt the creation of the timer to the main thread using dispatch_async(dispatch_get_main_queue(), ...)?
I'm simulating a device that takes a substantial amount of time to respond to a series of REST HTTP request (to support automated testing). I'm writing an OS X app to do this. I was trying to avoid running the timer on the main thread because I don't want the work the timer eventually spawns to get in the way of the main thread's execution. But, I guess that simply means I can dispatch it to another queue that I create? I don't need to serialize individual operations; it would be better if they could run concurrently, if that's how things worked out.
I guess I could add the timers on the main queue, and when they fire, enqueue their work on some other queue, but that seems to add some overhead to things. (It's negligible in this case, but this doesn't seem like the way to write a high-performance web server).
--
Rick Mann
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden