0

What happens when I use OSSpinLockLock when the lock is already held in the same thread? (hence it should "let me in").

I know it doesn't have a counter, but it's a problem to implement one, because then I'd need to verify this is the thread, the count is zero, and all this would probably need to be locked as well...

Vojtěch Melda Meluzín
  • 1,117
  • 3
  • 11
  • 22

1 Answers1

1

If you attempt to lock a spin lock from the thread that already owns it, you will deadlock. Spin locks are not recursive.

You should either look at pthread recursive mutexes, or change your design to avoid having to lock recursively.

Community
  • 1
  • 1
zneak
  • 134,922
  • 42
  • 253
  • 328
  • Thank you, it's just that I was looking for the spin locks to get better performance in the specific scenarios. Something like critical sections on Windows. – Vojtěch Melda Meluzín May 09 '14 at 10:40
  • @VojtěchMeldaMeluzín, Windows critical sections aren't recursive either. For the rest, spin locks are quick when you have low contention: otherwise, they waste CPU cycles. – zneak May 09 '14 at 14:30
  • Not true, Windows critical sections are recursive. See this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608(v=vs.85).aspx – Vojtěch Melda Meluzín May 09 '14 at 21:37
  • @VojtěchMeldaMeluzín, you're right. I looked it up before and read [this question](http://stackoverflow.com/questions/13117687/can-a-windows-critical-section-object-be-configured-to-deny-recursive-access) too fast and thought it meant the opposite. – zneak May 10 '14 at 12:16