Re: How to set up a thread listening to performSelector: messages?
Re: How to set up a thread listening to performSelector: messages?
- Subject: Re: How to set up a thread listening to performSelector: messages?
- From: "Michael Ash" <email@hidden>
- Date: Thu, 25 Sep 2008 12:06:59 -0400
On Thu, Sep 25, 2008 at 11:53 AM, Oleg Krupnov <email@hidden> wrote:
> I actually have tried this. My code looks like this (is it correct?):
>
> @implementation Worker
>
> - (void)threadMain:(id)data
> {
> runLoop = [NSRunLoop currentRunLoop];
> [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
> while(true)
> {
> [runLoop run];
> }
> }
>
> - (void)processRequest:(id)sender
> {
> NSLog(@"hello");
> }
>
> @end
>
> Then from the main thread I send:
>
> [[worker runLoop] performSelector:@selector(processRequest:)
> target:worker argument:request order:0 modes:[NSArray
> arrayWithObject:NSDefaultRunLoopMode]];
>
> This time the -[NSRunLoop run] method does not exit immediately.
> However the problem is that processRequest is never called and the
> -[NSRunLoop run] never exits.
>
> What can be wrong? Should it work like this at all?
You can't do this. NSRunLoop is not thread safe. Since it is always
being used by the thread which it manages, this means that you cannot
use it from other threads at all.
If you can require 10.5, you can use this method:
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr
withObject:(id)arg waitUntilDone:(BOOL)wait;
Otherwise, CFRunLoop is thread safe and does support this sort of
cross-thread invocation. You can also use Distributed Objects, use
that NSPort for messaging, or set up your own message queue system
using any number of mechanisms.
Mike
_______________________________________________
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