Re: Importance of thread safety and performance.
Re: Importance of thread safety and performance.
- Subject: Re: Importance of thread safety and performance.
- From: Pandaa <email@hidden>
- Date: Sat, 9 Jul 2005 19:55:46 +0200
9 jul 2005 kl. 16.21 skrev Jonathon Mah:
On 2005-07-09, at 14:57, アンドレ(Andre) wrote:
Ok, my first idea is to just make every accessor setter and
methods that alter data use NSLock to lock and unlock
any changed variables etc... My concern here is say [myObject
addObject:object] was called a large number of times,
all of the locking and unlocking would slow things down wouldn't it?
How many times? Locking should be absolutely negligible.
Depending on the kind of lock used, it would be far from negligible
in the context of a simple accessor. Wether that's globally
significant in the app is a different issue, but locally it's likely
to be a comparably large cost. The only way to find out if it will be
a performance problem is to write the code and profile it, do not let
fear stall your development! :)
But more importantly, having each and every accessor handle locking
may lead to larger code size and the need to carefully avoid
deadlocks when locking methods call each other or methods which
acquire several locks are called from several threads.
Unless your class is continually accessed from or coordinates work
for many different threads, it will probably be easier to handle the
locking from the outside of the class in objects that logically take
on a coordinating role. This will keep the interactions between
different locks simple, and when performing a series of primitive
operations it will be easy to handle intermediate object states that
should not be exposed to other threads.
Another idea is to make two methods like -threadSafeAddObject and -
addObject...
A possibly better alternative would be to have a method -[myObject
addObjectsFromArray:someArray], which will only need to lock one
lock, add everything, and then unlock it.
Sounds like a good suggestion.
I would rather do things "the right way" first than waste my time
later in upkeep...
That's a good attitude! :) But often there is no "right way" and even
if there is one it may not be easy at all to discover it at the
onset. Wasting time in upkeep may sometimes be the most efficient way
of discovering the right solution.
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. email@hidden . . www.synapticpulse.net .
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden