Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:28:57+00:00 2026-05-27T00:28:57+00:00

Assume I have the following code: let a = ref 4. printfn 1) a

  • 0

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 = 8

val a : float ref = {contents = 8.0;}
val t1 : System.Threading.Thread

unlocked

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:28:58+00:00Added an answer on May 27, 2026 at 12:28 am

    I agree with @Dmitry that using lock k (fun () -> ...) doesn’t mean that you prevent mutation on k; it does mean that you acquire a key k to 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 a arbitrarily. These values can be explained by the situation when one thread accessing the old value of a and the other thread trying to update that cell.

    let a = ref 4;;
    
    printfn "1) a = %i" !a
    
    let t1 = System.Threading.Thread (fun () ->  
        printfn "locked in thread 1"    
        a:= !a + 2
        printfn "unlocked in thread 1"    
        )
    
    let t2 = System.Threading.Thread (fun () ->  
        printfn "locked in thread 2"    
        a:= !a - 3
        printfn "unlocked in thread 2"    
        )
    
    t1.Start()
    t2.Start()
    
    System.Threading.Thread.Sleep 1000 // wait long enough to get the correct value
    printfn "2) a = %i" !a;;
    System.Console.ReadKey() |> ignore;;
    

    To ensure the correct result (which should be 3), you can introduce an object monitor, and any thread which would like to update a has to acquire monitor first:

    let monitor = new Object()
    let a = ref 4;;
    
    printfn "1) a = %i" !a
    
    let t1 = System.Threading.Thread (fun () ->  
        printfn "locked in thread 1"    
        lock monitor (fun () -> a:= !a + 2)
        printfn "unlocked in thread 1"    
        )
    
    let t2 = System.Threading.Thread (fun () ->  
        printfn "locked in thread 2"    
        lock monitor (fun () -> a:= !a - 3)
        printfn "unlocked in thread 2"    
        )
    
    t1.Start()
    t2.Start()
    
    System.Threading.Thread.Sleep 1000 // wait long enough to get the correct value
    printfn "2) a = %i" !a;;
    System.Console.ReadKey() |> ignore;;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have the following code: public class MainClass { public static void
let's assume we have the following code: public class TestScope { private int a
I have the following question: Let's assume the code below: ... <h:form id=..> <table>
Let's assume I have a file named Main.java with the following code: public class
Let's assume you have a table and the following Jquery code var rows =
Lets assume we have the following code: abstract class Base1 { protected int num;
Assume that we have the following code: class Program { static volatile bool flag1;
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's assume we have the following method in the business layer. What's the best
I have a question regarding string modification. Let's assume that we have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.