Re: NSLock vs. NSRecursiveLock
Re: NSLock vs. NSRecursiveLock
- Subject: Re: NSLock vs. NSRecursiveLock
- From: Wade Tregaskis <email@hidden>
- Date: Fri, 15 Oct 2004 18:40:37 +1000
If I understand correctly, if a thread holds an NSLock and attempts to
acquire it again, the thread will freeze. A solution is to use
NSRecursiveLock instead, because the thread can acquire the lock
multiple times without running into a deadlock.
So, is it safer to use NSRecursiveLock instead of NSLock? Are there
instances where it's better to use NSLock over NSRecursiveLock? Are
there any performance advantages perhaps?
A "normal" lock is always going to be as fast or faster than a
recursive lock (this is implementation & platform dependent; I believe
Apple's implementations use pthread mutexes, which are slower when
recursive).
Generally it's safer *not* to use a recursive lock. Often any sort of
recursion is a bug in your program, and it's better to catch these
straight up than risk data corruption (which will invariably be a lot
harder to trace back to the locking).
You only use a recursive lock in those situations where what you're
locking around is implicitly safe from multiple accesses within the
same thread. This is often the case, but can be tricky to confirm. It
just requires the relevant code to ensure all entries and exits to it
(including function calls made by it) leave the relevant data in a
deterministic and safe state. But then, it only takes one
inexperienced person working with the code to not follow this rule, and
you can have all manner of tricky problems.
Wade Tregaskis (AIM, Yahoo & Skype: wadetregaskis, ICQ: 40056898, MSN &
email: email@hidden, Jabber:
email@hidden
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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