Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Threading
- let mkThread f = Thread (ThreadStart f)
- let startThread f = (mkThread f).Start()
- let waitFor obj = ignore(Monitor.Wait obj)
- let wakeWaiters obj = Monitor.PulseAll obj
- let numClients = 5
- let blahblah = ref false
- type Client(clientID:int) =
- let clients:Client[] ref = ref Array.empty
- let inCharge = ref (if clientID=0 then true else false)
- let usingNow = ref false
- member this.ClientID = clientID
- member this.init(theClients) = clients:=theClients
- startThread <| fun () ->
- for i in 1..10 do
- this.NewRequest i
- printfn "Client %d: DONE WITH EVERYTHING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" clientID
- member this.NewRequest exp = // Makes the client do a new request
- lock this <| fun()->
- while(not (!inCharge)) do
- inCharge := !clients |> Array.exists (fun cl -> cl.CanIHaveIt this.ClientID)
- usingNow:=true
- printfn "Client %d: took charge" clientID
- Thread.Sleep 100 // Only one client should do this at a time.
- usingNow:=false
- printfn "Client %d: Finished exp %d" clientID exp
- lock blahblah <| fun()-> wakeWaiters blahblah
- member this.CanIHaveIt clID = // Returns true when giving to the caller
- lock blahblah <| fun()-> // printfn "Client %d: Request from client %d" clientID clID
- while (!usingNow) do waitFor blahblah
- let wasInCharge = !inCharge
- inCharge:=false
- wasInCharge
- let clients = Array.map (fun clID -> Client(clID)) [|0..numClients-1|]
- clients |> Array.iter (fun cl -> cl.init clients)
Add Comment
Please, Sign In to add comment