On Thu, Jan 8, 2009 at 11:08 AM, Scott Ribe <scott_r...@killerbytes.com> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to