相关疑难解决方法(0)

.NET 4 SpinLock

以下测试代码(F#)未返回我期望的结果:

let safeCount() =
  let n = 1000000
  let counter = ref 0
  let spinlock = ref <| SpinLock(false)
  let run i0 i1 () =
    for i=i0 to i1-1 do
      let locked = ref false
      try
        (!spinlock).Enter locked
        if !locked then
          counter := !counter + 1
      finally
        if !locked then
          (!spinlock).Exit()
  let thread = System.Threading.Thread(run 0 (n/2))
  thread.Start()
  run (n/2) n ()
  thread.Join()
  !counter
Run Code Online (Sandbox Code Playgroud)

我希望SpinLock相互排除计数器,因此,它返回1,000,000的计数,但相反,它返回较小的值,好像没有发生互斥.

有什么想法有什么不对吗?

.net f#

5
推荐指数
3
解决办法
870
查看次数

标签 统计

.net ×1

f# ×1