lock(syncObject) 什么时候可以抛出异常?
我在 .NET 中编写了一个 com 组件,如果我尝试锁定任何方法中的任何对象(由与我的 com 组件通信的非托管代码调用),我会收到异常。
我目前没有异常的确切文本,但它也没有多大帮助。
所以我的问题是在什么情况下 lock(syncObject) 会抛出异常? 以下是一些事实:
- 同步对象不为空
- 同步对象尚未锁定
它与在 STA(单线程单元)或 MTA(多线程单元)中运行的被调用者有什么关系吗?
I have written a com component in .NET and if I try to take a lock on any object in any method (that is invoked by unmanaged code talking to my com component) I get an exception.
I do not have the exact text of the exception at this moment but it wasn't much helpful either.
So my question is under what circumstances a lock(syncObject) would throw an exception?
Here are some facts:
- syncObject is not null
- syncObject is not already locked
Would it have anything to do with callee running in STA (Single Threaded Apartment) or MTA (Multi Threaded Apartment)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自此页面:
正如您所读到的,似乎在资源受限的情况下,我们可能会从 Lock(o) 内部使用的 Monitor 的 Enter 方法抛出异常。
那么也许您的解决方案类似于旋转等待?
cond 可以
与例如 Interlocked.CompareExchange 一起使用,您可以将其更改为例如 Thread.Current.ManagedThreadID 或其他非零值?
From this page:
So as you can read, it seems that under resource constrained conditions we might get exceptions thrown from the Enter method of Monitor which lock(o) uses internally.
So perhaps your solution is something like a spin-wait?
Where cond could be some
used with e.g. Interlocked.CompareExchange where you change to e.g. Thread.Current.ManagedThreadID or something else non-zero?