Assume I have the following code:
let a = ref 4.
printfn "1) a = %g" !a
let t1 = System.Threading.Thread (fun () ->
lock a (fun () ->
printfn "locked"
System.Threading.Thread.Sleep 1000
printfn "unlocked") )
t1.Start()
System.Threading.Thread.Sleep 100
a := 8.
printfn "2) a = %g" !a
Which gives the following result:
1) a = 4
locked
2) a = 8val a : float ref = {contents = 8.0;}
val t1 : System.Threading.Threadunlocked
Why does a equals 8. when I locked it? Is it possible to lock a record with mutable values and refs?
PS: I need to lock an object which is both accessed by me and at the same time by WCF.
I agree with @Dmitry that using
lock k (fun () -> ...)doesn’t mean that you prevent mutation onk; it does mean that you acquire a keykto access an object. Because the key is unique, you have mutual exclusive access to the object to avoid getting wrong results.Based on your example, running this below code in Debug mode results in results of 1, 3 or 6 for
aarbitrarily. These values can be explained by the situation when one thread accessing the old value ofaand the other thread trying to update that cell.To ensure the correct result (which should be 3), you can introduce an object
monitor, and any thread which would like to updateahas to acquiremonitorfirst: