• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSLock optimizations
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSLock optimizations


  • Subject: Re: NSLock optimizations
  • From: Ondra Cada <email@hidden>
  • Date: Sun, 25 Aug 2002 11:22:38 +0200

On Sunday, August 25, 2002, at 10:01 , Steve Gehrman wrote:

My question is: Is there a way to structure this code so it is fully thread safe.

without locking the check, there is not: obviously, whatever you structure the code, you need somehow to check the variable -- and another thread might shoot between the test and its result!

The thing I'm trying to avoid is constantly calling lock and unlock every time this function is called once the variable has been successfully initialized. Is calling lock and unlock slow?

Not much, but of course some penalty there is. Depends on the code.

Maybe the answer is to just call lock and unlock everytime?

I can see two possible optimizations:

(a) allocate the lock only when needed (ie. when the app went multithreaded *AND* the variable was initialized). Till that, let it be nil, which means nothing would be called. Beware that you probably would need another lock in the initialization, lest different threads allocate different locks!

(b) go inline assembly to make your own extremely simple lock (I believe with PPC it can be done with one specific instruction -- don't remember details, see PPC reference). I would seriously discommend this, but if you *do* need *serious* optimization, it might be a way. Very definitely though don't do this unless profiling proved that locking is your bottleneck indeed!
---
Ondra Hada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.
References: 
 >NSLock optimizations (From: Steve Gehrman <email@hidden>)

  • Prev by Date: NSLock optimizations
  • Next by Date: Getting the IP address with remote DO
  • Previous by thread: NSLock optimizations
  • Next by thread: Re: NSLock optimizations
  • Index(es):
    • Date
    • Thread