On Mon, Dec 22, 2008 at 10:22 AM, Ken Tozier <kentoz...@comcast.net> wrote: > How would one share a lock? Should I make it a property of the class? And > then what? According to the NSLock documentation, multiple calls to tryLock > are a no no, so how does one determine the current state of a lock? I didn't > see any methods like "isLocked"
I hate to say this, but You Are Doing It Wrong. Asking how to determine the current state of a lock is asking the wrong question. Determining the current state of the lock is (almost) never useful. It's certainly never useful here. Determining the current state of the lock is like determining the current state of a stoplight, only you're looking at the state of the light for the intersecting road. If it's green, obviously stop. If it's red... well, who knows? Instead what you should do is simply stop your car if *your* light is red, and continue if *your* light is green. The way you do this is to lock and unlock. You simply do [lock lock], then [lock unlock]. In the middle, you put the stuff that needs to be protected. The system takes care of ensuring that only one person at a time is allowed in the middle. You don't have to worry about how it works or what state the lock is in or anything of the sort. Lock, modify or read, unlock. That's it. And note that *every access to any shared data* must be locked. You can't just lock the "threaded" access. The moment any data is shared between threads, *all* accesses *anywhere* must be locked. My personal recommendation would be to just copy the array and give up on locking. Pass the copy into your thread and then you have no shared data. Shared data is the bane of threading. The moment two threads share data, the potential for misfortune goes up by a factor of at least a million. Avoid it whenever you can. The cost of copying an array is irrelevant by comparison. 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