Skip navigation.
 
mlRe: Using NSThreads in command-line apps
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

Related mailsAuthorDate
mlUsing NSThreads in command-line apps ben syverson May 15, 20:42
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 20:47
mlRe: Using NSThreads in command-line apps Randall Meadows May 15, 20:49
mlRe: Using NSThreads in command-line apps Uli Kusterer May 15, 20:51
mlRe: Using NSThreads in command-line apps Jens Alfke May 15, 20:52
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:16
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:17
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 21:25
mlRe: Using NSThreads in command-line apps Bill Bumgarner May 15, 21:31
mlRe: Using NSThreads in command-line apps ben syverson May 15, 21:53
mlRe: Using NSThreads in command-line apps Jens Alfke May 15, 23:17
mlRe: Using NSThreads in command-line apps ben syverson May 16, 00:28
mlRe: Using NSThreads in command-line apps Sherm Pendley May 16, 00:31
mlRe: Using NSThreads in command-line apps Hamish Allan May 16, 00:33
mlRe: Using NSThreads in command-line apps ben syverson May 16, 00:40
mlRe: Using NSThreads in command-line apps Hamish Allan May 16, 01:06
mlRe: Using NSThreads in command-line apps Jens Alfke May 16, 03:02
mlRe: Using NSThreads in command-line apps ben syverson May 16, 06:50