• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to set up a thread listening to performSelector: messages?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >How to set up a thread listening to performSelector: messages? (From: "Oleg Krupnov" <email@hidden>)
 >Re: How to set up a thread listening to performSelector: messages? (From: Ken Thomases <email@hidden>)
 >Re: How to set up a thread listening to performSelector: messages? (From: "Oleg Krupnov" <email@hidden>)

  • Prev by Date: Filtering an NSArrayController using a to-many relationship
  • Next by Date: Re: How to set up a thread listening to performSelector: messages?
  • Previous by thread: Re: How to set up a thread listening to performSelector: messages?
  • Next by thread: Re: How to set up a thread listening to performSelector: messages?
  • Index(es):
    • Date
    • Thread