NSLock - should just block when locking a locked lock?
Asked Answered
J

2

8

I have a loop which starts with a

[lock lock];

because in the body of the loop I am creating another thread which needs to finish before the loop runs again. (The other thread will unlock it when finished).

However on the second loop I get the following error:

2011-02-02 07:15:05.032 BLA[21915:a0f] *** -[NSLock lock]: deadlock (<NSLock: 0x100401f30> '(null)')
2011-02-02 07:15:05.032 BLA[21915:a0f] *** Break on _NSLockError() to debug.

The "lock" documentation states the following:

Abstract: Attempts to acquire a lock, blocking a thread’s execution until the lock can be acquired. (required)

which makes me think it would just block until the lock could be acquired?

J answered 1/2, 2011 at 21:28 Comment(0)
P
9

Sounds like two problems:

  • Locking a lock on one thread and unlocking on another is not supported – you probably want NSCondition. Wait on the NSCondition in the parent thread, and signal it in the child thread.
  • A normal NSLock can’t be locked while already locked. That’s what NSRecursiveLock is for.
Perrault answered 1/2, 2011 at 21:51 Comment(0)
T
3

Did you remember to send -unlock when you were done? Each call to -lock must be paired with a call to -unlock.

Transistor answered 1/2, 2011 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.