FROM : Jens Alfke
DATE : Thu May 15 23:17:56 2008
On 15 May '08, at 12:16 PM, ben syverson wrote:
> I don't want to listen to any input, and I want the loop to execute
> as fast as possible. However, NSRunLoop says I need to set a timer
> or an input...
If you're just running a loop of your own code (or blocking in a
system call like kqueue) there's no way for -
performSelectorOnMainThread to send an event to that thread. (It would
have to use something asynchronous like a signal, which is really
nasty to deal with.)
NSRunLoop provides the outer-level loop for a thread, and
(effectively) manages an event queue for it. To do that, it has to be
in control of the thread. Foundation and AppKit are built on top of
this model, using asynchrony instead of blocking, and generally not
relying much on multi-threading.
> I have one thread which runs the server's infinite loop. It can't be
> the main thread, because it calls kevent, which doesn't return a
> value until there's an event. So that thread's loop stops while
> kevent is waiting.
You can wire up kqueues to a runloop fairly easily. Instead of
blocking, the kqueue will post an event to the runloop when something
happens. Then instead of waiting in kevent, you just handle events in
your runloop, and your callback will be invoked as necessary.
I used this technique in PubSub; I believe I got the information from
the "Advanced Mac OS X Programming" book.
(My advice: unless you're seriously trying to write The Fastest Web
Server In The World, use NSStream and/or CFNetwork and follow their
asynchronous/runloop model.)
—Jens
DATE : Thu May 15 23:17:56 2008
On 15 May '08, at 12:16 PM, ben syverson wrote:
> I don't want to listen to any input, and I want the loop to execute
> as fast as possible. However, NSRunLoop says I need to set a timer
> or an input...
If you're just running a loop of your own code (or blocking in a
system call like kqueue) there's no way for -
performSelectorOnMainThread to send an event to that thread. (It would
have to use something asynchronous like a signal, which is really
nasty to deal with.)
NSRunLoop provides the outer-level loop for a thread, and
(effectively) manages an event queue for it. To do that, it has to be
in control of the thread. Foundation and AppKit are built on top of
this model, using asynchrony instead of blocking, and generally not
relying much on multi-threading.
> I have one thread which runs the server's infinite loop. It can't be
> the main thread, because it calls kevent, which doesn't return a
> value until there's an event. So that thread's loop stops while
> kevent is waiting.
You can wire up kqueues to a runloop fairly easily. Instead of
blocking, the kqueue will post an event to the runloop when something
happens. Then instead of waiting in kevent, you just handle events in
your runloop, and your callback will be invoked as necessary.
I used this technique in PubSub; I believe I got the information from
the "Advanced Mac OS X Programming" book.
(My advice: unless you're seriously trying to write The Fastest Web
Server In The World, use NSStream and/or CFNetwork and follow their
asynchronous/runloop model.)
—Jens






Cocoa mail archive

