使用互锁

发布于 2024-08-20 23:54:23 字数 387 浏览 6 评论 0原文

这段代码是线程安全的吗?或者这样说:

是否有办法调用 GetIt() 并且 GetIt() 会将相同的数字返回给 2 个不同的线程

Private Shared hitCount As Long = 1

Public Shared Function GetIt() As Long
     Threading.Interlocked.Increment(hitCount)
     DoSomethingQuick(hitCount)
     Return hitCount
End Function

看起来这是可能的,那么我应该使用 Interlocked.Read() 还是将整个内容锁定在一个块中?

Is this code thread-safe? or put it this way:

Is there anyway to call GetIt() and that GetIt() will return the same number to 2 different threads

Private Shared hitCount As Long = 1

Public Shared Function GetIt() As Long
     Threading.Interlocked.Increment(hitCount)
     DoSomethingQuick(hitCount)
     Return hitCount
End Function

It seems like it's possible, then am I supposed to use Interlocked.Read() or lock the whole thing in one block?

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

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

发布评论

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

评论(1

海螺姑娘 2024-08-27 23:54:23

是的,有一种可能性:

  1. 线程 1 运行 Threading.Interlocked.Increment(hitCount)
  2. 线程 2 运行 Threading.Interlocked.Increment(hitCount)
  3. 线程 1 运行 返回 hitCount
  4. 线程 2 运行 返回 hitCount

在步骤 3 和 4 中,hitCount 将是相同的值。

但解决方法很简单 Interlocked.Increment 返回增量值,因此只需将代码更改为:

Private Shared hitCount As Long = 1L

Public Shared Function GetIt() As Long
     Return Threading.Interlocked.Increment(hitCount)
End Function

编辑
或者现在根据您的编辑,您有一个相当大的时间漏洞。不管怎样,这就是你想要的:

Public Shared Function GetIt() As Long
     Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
     Console.Writeline("Something, something....")
     Return localHitCount 
End Function

编辑
然后这样做(这正是迈克尔在下面建议的)

Private Shared hitCount As Long = 1L

Public Shared Function GetIt() As Long
     Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
     DoSomethingQuick(localHitCount )
     Return localHitCount 
End Function

Yes there is a possibility:

  1. Thread 1 runs Threading.Interlocked.Increment(hitCount)
  2. Thread 2 runs Threading.Interlocked.Increment(hitCount)
  3. Thread 1 runs Return hitCount
  4. Thread 2 runs Return hitCount

In steps 3 and 4, hitCount will be the same value.

But the fix is easy Interlocked.Increment returns the incremented value, so just change your code to:

Private Shared hitCount As Long = 1L

Public Shared Function GetIt() As Long
     Return Threading.Interlocked.Increment(hitCount)
End Function

Edit
Or now based on your edit, you have a pretty bit timing hole. Anyway then this is what you want:

Public Shared Function GetIt() As Long
     Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
     Console.Writeline("Something, something....")
     Return localHitCount 
End Function

Edit
Then do this (which is exactly what Michael suggested below)

Private Shared hitCount As Long = 1L

Public Shared Function GetIt() As Long
     Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
     DoSomethingQuick(localHitCount )
     Return localHitCount 
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文