Re: Threads and locks
Re: Threads and locks
- Subject: Re: Threads and locks
- From: Chris Hanson <email@hidden>
- Date: Thu, 1 Apr 2004 17:49:31 -0600
On Apr 1, 2004, at 3:25 PM, Mark's Studio wrote:
i only use one NSLock is that enough, or do i need to have a seperate
lock for every object?
what should i do to make this thread safe
[array1 setArray:array2]
both arrays are NSMutableArray
The short, moderately snippish answer is that you need to understand
why you're using locks.
Here's Apple's documentation on Cocoa multithreading:
<
http://developer.apple.com/documentation/Cocoa/Conceptual/
Multithreading/Tasks/foundation.html>
Specifically, you need to make sure that you're only changing array1
from within a single thread at once. This is called a critical
section, and you create one with an NSLock or with the @synchronized
construct (new in GCC 3.3 on Panther). If you create your own NSLock,
you'll need to pair it with the array and always have a 1:1
correspondence between the NSLock object and the object it controls
access to. You'll also need to be sure to lock when reading as well,
since you don't want to be reading from it while it's in an
inconsistent state.
The most common cause of bad access exceptions, though, is releasing an
object too many times. Probably the most common reason for this is
using a convenience constructor to instantiate an object, and then not
retaining it for use later. Are you using +[NSArray
arrayWithCapacity:] to instantiate the array you're manipulating, and
then not retaining it?
-- Chris
--
Chris Hanson <email@hidden>
http://www.livejournal.com/users/chanson/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.