NSLock - 在锁定锁定的锁时应该阻塞吗?

发布于 2024-10-15 05:59:37 字数 473 浏览 6 评论 0原文

我有一个以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-10-22 05:59:37

听起来像两个问题:

  • 不支持在一个线程上锁定锁并在另一个线程上解锁 - 您可能需要 NSCondition。在父线程中等待 NSCondition,并在子线程中向其发出信号。
  • 普通的 NSLock 在已经锁定的情况下无法被锁定。这就是 NSRecursiveLock 的用途。

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.
清欢 2024-10-22 05:59:37

完成后您是否记得发送-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文