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: Scott Ribe <email@hidden>
- Date: Thu, 08 Jan 2009 09:08:32 -0700
- Thread-topic: Returning value from detached NSThread selector with multiple arguments.
> 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.
Right, in general, but I don't see any problem with the proposed use, since
there's adequate synchronization implicitly:
- Main thread sets up dictionary, retains it, spawns secondary thread.
- 2nd thread retains the dictionary, reads parameters, does its stuff, adds
results to the dictionary, releases the dictionary, calls
performSelector:onMainThread.
- Main thread reads results, releases the dictionary.
Note that the retain/release in the 2nd thread is really only necessary if
there's a possibility that the main thread will lose interest in the results
and release the dictionary before the 2nd thread finishes.
Re your caution in particular, the main thread must not "peek" in the
dictionary (to look at partial results for instance) before the 2nd thread
has called back, because that look into the dictionary could happen in the
middle of an update when its internal state is inconsistent.
Any attempt to monitor progress would require either explicit
synchronization on the dictionary, or more simply just use
performSelector:onMainThread: with a new object allocated and set up in the
2nd thread and released on the main thread for each call. And my personal
preference would be to return results that way as well, rather than reuse
the dictionary, because it doesn't seem simpler in any way to use a single
dictionary for both purposes (unless the main thread, after the 2nd thread
has finished, would need access to the parameters for UI display or
whatever).
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
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