• 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: Threads, messages and blocking
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threads, messages and blocking


  • Subject: Re: Threads, messages and blocking
  • From: Tom Sutcliffe <email@hidden>
  • Date: Thu, 29 May 2003 23:07:57 +0100

I have an Objective-C Cocoa application with two threads (NSThread). I need
thread 1 to tell thread 2 to do something and to know when thread 2 is done
doing it. But thread 1 must not block while thread 2 carries out its work
(if blocking was ok, I wouldn't even need a second thread).

Short answer: in the second thread, you probably want to call NSObject's performSelectorOnMainThread.

Long answer: It depends on what work you want thread 1 to be able to do while thread 2 is running, and what thread 1 actually is. If it is the main application thread, then you use performSelectorOnMainThread, which is Cocoa's way of doing callbacks. Thread 1 can continue to run the UI etc while thread 2 is doing its stuff. When thread 2 is finished it makes a performSelectorOnMainThread call, which puts a "I'm finished" message in the event queue that thread 1 will then see next time the application idles. This is the easiest solution.

Alternatively if thread1 is some other thread, then you might want to look at the solution Dave Camp just posted, which seems perfect for the general case.

It seems that
setting up an NSConnection between the threads (as demonstrated in the Apple
"Forming Connections Between Threads" document) won't solve my problem
because thread 1 will send the message to thread 2 and then block while the
work is carried out (until the called method returns). Right?

I wouldn't use NSConnections unless you're doing something that isn't handled by the situation below. They are probably a bit low level for what you want.

I looked at NSDistributedNotificationCenter as a possible alternative.
Sending notifications between the objects would at least get rid of
the blocking problem. But it's not clear to me whether that is the
right way to go or if it will even work in the first place. The docs
say that NSDistributedNotificationCenter should not be used to communicate
between threads "within the same task." I'm not clear on what the
definition of "task" in this context. Does that mean the same "process"?

Yes, I think it means process - because the DistributedNotificationCenter can't guarantee what thread will be executing the notification. Basically it wouldn't do what you wanted.

Regards,

Tom
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Threads, messages and blocking
      • From: Sean Harding <email@hidden>
References: 
 >Threads, messages and blocking (From: Sean Harding <email@hidden>)

  • Prev by Date: Re: NSString from Carbon by AppleEvent
  • Next by Date: color
  • Previous by thread: Re: Threads, messages and blocking
  • Next by thread: Re: Threads, messages and blocking
  • Index(es):
    • Date
    • Thread