问题描述
我想同时SyncLock多个对象,例如:
SyncLock objA,objB
''使用objA和objB做东西
结束SyncLock
但是只有两个人才能获得锁定对象可以同时锁定
。
当多个线程有条件地锁定这些
对象时我想防止可能的死锁订单。
这可能吗?如果有的话怎么样?
谢谢,
弗兰克
Hi,
I want to simultaneously SyncLock multiple objects, something like:
SyncLock objA, objB
'' do stuff with objA and objB
End SyncLock
but so that a lock is only obtained if BOTH objects can be locked obtained
simultaneously.
I want to prevent possible deadlocks when multiple threads lock these
objects conditionally and out of order.
Is that possible and if so how?
Thanks,
Frank
推荐答案
如果另一个线程这样做可能导致致命的拥抱:
SyncLock objB
SyncLock objA
结束SyncLock
结束SyncLock
That could lead to a deadly embrace if another thread does this:
SyncLock objB
SyncLock objA
End SyncLock
End SyncLock
确实是死锁。
但是,我能想到的唯一另一件事是封装需要锁定到一个对象并锁定它的对象
。
Deadlock indeed.
However, the only other thing I can think of is to encapsulate the objects
that require locking into one object and to lock that.
这篇关于多对象SyncLock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!