空对象上的 SyncLock

发布于 2024-10-26 20:14:19 字数 283 浏览 5 评论 0原文

有没有简单的方法来同步锁定可以为空的对象?

在您提出要求之前,是的,我知道对 null 变量执行 SyncLock 是不合逻辑的。但是,这会简化我的代码,因为现在我别无选择,只能在调用 SyncLock 之前对整个地方执行空检查。

If myObjectA Is Nothing Then
  myObjectA = myObjectB
Else
  SyncLock myObjectA
    myObjectA = myObjectB
  End SyncLock
End If

Is there simple way to SyncLock an object that can be null?

And before you ask for it, yes, I know that it isn't logical to perform a SyncLock on a null variable. However, that would simplify my code because right now, I have no choice but to perform a null check all over the place before calling a SyncLock.

If myObjectA Is Nothing Then
  myObjectA = myObjectB
Else
  SyncLock myObjectA
    myObjectA = myObjectB
  End SyncLock
End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

Smile简单爱 2024-11-02 20:14:19

是的,使用 Helper 对象。

当 myObjectA 用作锁时,无论如何您都不应该对其进行分配。

来自 MSDN

锁定对象值。的价值
锁定对象不能为 Nothing。你必须
使用前创建锁对象
它在 SyncLock 语句中。

您无法更改以下值
执行 SyncLock 时锁定对象
堵塞。该机制要求
锁定对象保持不变。

Yes, use a Helper object.

You shouldn't assign to myObjectA anyway when it's used as a Lock.

From MSDN:

Lock Object Value. The value of
lockobject cannot be Nothing. You must
create the lock object before you use
it in a SyncLock statement.

You cannot change the value of
lockobject while executing a SyncLock
block. The mechanism requires that the
lock object remain unchanged.

剩一世无双 2024-11-02 20:14:19

不,您不能使用空引用作为锁的标识符。

如果引用可以为空,您甚至不能使用引用作为标识符,因此您当前的代码不是线程安全的。您必须使用不同的方式来识别锁。两个不同的线程可以替换空引用而无法相互排除,这将导致一个引用被另一个引用覆盖:

If myObjectA Is Nothing Then
  ' Here another thread can change the reference, believing that it's safe
  myObjectA = myObjectB
Else
  SyncLock myObjectA
    myObjectA = myObjectB
  End SyncLock
End If

No, you can't use a null reference as identifier for a lock.

You can't even use a reference as identifier if it can be null, so your current code is not thread safe. You have to use a different way to identify the lock. Two different threads can replace the null reference without being able to exclude each other, which would cause one reference to be overwritten by the other:

If myObjectA Is Nothing Then
  ' Here another thread can change the reference, believing that it's safe
  myObjectA = myObjectB
Else
  SyncLock myObjectA
    myObjectA = myObjectB
  End SyncLock
End If
够钟 2024-11-02 20:14:19

可能有一些重构可以帮助避免这些情况。这段代码看起来很奇怪。如果为 null 则将一个对象分配给 lock 变量,如果不是则锁定,这似乎有些错误。另外,您锁定然后更改锁定变量!

请记住,锁定是针对引用而不是值!基本上,它的作用是阻止所有不在锁内的代码块对指定引用的访问。

There is probably some refactoring that would help avoid these situations. This code seems wierd. asigning to the lock variable an object if null and locking if not seems somehow wrong. Plus you lock and then change the lock variable!

Remember that locking goes to the reference not the value! Basically what it does is block acess to the specified reference from all blocks of code not inside the lock.

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