Re: Returning value from detached NSThread selector with multiple arguments.
Re: Returning value from detached NSThread selector with multiple arguments.
- Subject: Re: Returning value from detached NSThread selector with multiple arguments.
- From: Ken Thomases <email@hidden>
- Date: Wed, 7 Jan 2009 19:48:45 -0600
On Jan 7, 2009, at 7:28 PM, John Cassington wrote:
After testing, the dictionary concept works perfectly! Thanks!
Careful! Unless you're synchronizing access to the mutable
dictionary, this is not thread-safe. Even if it appears to work in
some cases, it's likely to blow up in your face in real-world use.
With regard to the originating thread retaining a reference to the
dictionary, is there a way for the original thread to know when
processing has completed on the secondary thread?
I would like my method to be run on a separate thread, but be able to
return a value to the caller in the normal fashion.
This last sentence doesn't make any sense. Why put the work on a
separate thread if you're just going to block waiting for it to
complete anyway? And the answer is, no you can't do that. It's the
difference between a detached vs. a joinable thread. Cocoa doesn't
have any direct support for joinable threads, although the underlying
pthreads API does.
I would recommend that your originating thread detach the worker
thread and then return to its caller and probably back to its run loop
(the event loop, if it's the main thread). When the worker thread is
finished, you can use -performSelectorOnMainThread:... or -
performSelector:onThread:... to invoke a method on the originating
thread. That method would be the continuation of the process of the
originating thread -- it would receive the result of the work as a
parameter and do whatever is appropriate with it.
Regards,
Ken
_______________________________________________
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