Re: "Use them from only one thread at a time"
Re: "Use them from only one thread at a time"
- Subject: Re: "Use them from only one thread at a time"
- From: "Stephen J. Butler" <email@hidden>
- Date: Thu, 19 May 2011 16:26:24 -0500
On Thu, May 19, 2011 at 3:56 PM, Sean McBride <email@hidden> wrote:
> On Thu, 19 May 2011 13:22:32 -0700, Jerry Krinock said:
>
>>"Thread-Unsafe Classes. The following classes and functions are
>>generally not thread-safe. In most cases, you can use these classes from
>>any thread as long as you use them from only one thread at a time."
>>
>>I'm confused by the second sentence.
>
> Indeed. Surely they don't mean that.
Surely they do mean that, and people do it all the time. The key is to
use locks to synchronize your access. For example, an
NSMutableDictionary is thread unsafe. But if you do this:
@synchronize (myDict) {
// read and write operations
}
Then it is OK to access myDict from multiple threads. But because of
the @synchronize, only one thread can be using it at a time.
Note: read operations on many container classes are thread safe. BUT
only if the class cannot be written to. If you are doing to both read
and mutate a class, then all operations (read and mutate) need to be
safe. You could do something like this:
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
// write to myDict
// spawn a bunch of worker threads that only read from myDict
// wait for all threads to complete
// write some more to myDict
_______________________________________________
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