Le 7 déc. 2011 à 06:10, Ken Thomases a écrit :
> On Dec 6, 2011, at 10:05 PM, Don Quixote de la Mancha wrote:
>> Contrary to Ken Thomases' assertion, there are all kinds of reasons to
>> use atomic operations as locking primitives. One is that they cannot
>> result in process context switches; if you are certain that you will
>> release the lock quite quickly, it is a whole lot faster, and uses
>> less memory, to use atomic arithmetic.
> You still shouldn't implement it manually using atomic increment and decrement. You should use OSSpinLock if that's what you're attempting. Which was my point. The original code was horrible and horribly misguided (and suffers from a race condition as others have pointed out, which is virtually inevitable when people try to reimplement synchronization instead of using ready-made synchronization primitives).
A better solution would be to use posix mutex and completely avoid OSSpinLock.
There is really few situations where SpinLock give you any benefit, as mutex implementation already try to spin a couple of time before locking. So if there is no contention, they are both very cheap and fast, and if there is contention, the mutex is better as it don't consume CPU waiting for other threads.
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden