let functionWithSharedCounter =
let counter = ref 0
fun () ->
// I tried the ones below:
// let index = Interlocked.Increment(&counter)
// let index = Interlocked.Increment(&!counter)
// let index = Interlocked.Increment(&counter.Value)
printfn "captured value: %d" index
functionWithSharedCounter ()
functionWithSharedCounter ()
functionWithSharedCounter ()
干杯,
答案 0 :(得分:2)
F#会自动将ref
类型的值视为byref
参数,因此您不需要任何特殊语法:
let functionWithSharedCounter =
let counter = ref 0
fun () ->
let index = Interlocked.Increment(counter)
printfn "captured value: %d" index
您也可以参考可变字段,因此您也可以编写以下内容:
let index = Interlocked.Increment(&counter.contents)
这适用于归档contents
,但不适用于counter.Value
,因为这是属性。