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: "Michael Ash" <email@hidden>
- Date: Thu, 8 Jan 2009 12:13:31 -0500
On Thu, Jan 8, 2009 at 11:08 AM, Scott Ribe <email@hidden> wrote:
>> 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.
May as well ditch the shared dictionary altogether if you're doing
this. Instead follow this plan:
- Main thread sets up dictionary, spawns secondary thread and passes
it as an argument.
- Secondary thread uses its argument to do its calculations.
- When it's done, it constructs a response dictionary, calls
performSelectorOnMainThread:... to hand it back.
By removing shared mutable data you remove a huge potential for things
to go wrong with your threads.
> 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.
It's actually not necessary at all. NSThread, like any other Cocoa
class, knows about Cocoa memory management and will retain/release the
thread object and arguments appropriately. (As will
performSelectorOnMainThread:.)
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