NSLock - 在锁定锁定的锁时应该阻塞吗?
我有一个以 a 开头的循环,
[lock lock];
因为在循环体中我正在创建另一个线程,该线程需要在循环再次运行之前完成。 (完成后另一个线程将解锁它)。
但是,在第二个循环中,我收到以下错误:
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.
“lock”文档声明如下:
摘要:尝试获取锁,阻塞线程的执行,直到获取锁为止。 (必填)
这让我认为它会阻塞直到获得锁?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来像两个问题:
NSCondition
。在父线程中等待 NSCondition,并在子线程中向其发出信号。NSLock
在已经锁定的情况下无法被锁定。这就是 NSRecursiveLock 的用途。Sounds like two problems:
NSCondition
. Wait on theNSCondition
in the parent thread, and signal it in the child thread.NSLock
can’t be locked while already locked. That’s whatNSRecursiveLock
is for.完成后您是否记得发送
-unlock
?每次对-lock
的调用都必须与对-unlock
的调用配对。Did you remember to send
-unlock
when you were done? Each call to-lock
must be paired with a call to-unlock
.