Advertisement
Guest User

Untitled

a guest
May 31st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.90 KB | None | 0 0
  1. module Logic
  2.  
  3. open System.Collections
  4. open System.Threading
  5.  
  6.     type Blocking_Queue () =
  7.         let mutable local_queue = Queue()
  8.        
  9.         let resetEvent = new AutoResetEvent(true)
  10.  
  11.         member this.add element =
  12.             lock this (fun () -> if (Interlocked.CompareExchange(ref local_queue.Count, 0, 0) = 0)
  13.                                  then resetEvent.Set() |> ignore
  14.                                  local_queue.Enqueue(element))
  15.  
  16.         member this.getFirstElement =
  17.             if (Interlocked.CompareExchange(ref local_queue.Count, 0, 0) = 0)
  18.                 then resetEvent.WaitOne() |> ignore
  19.                      local_queue.Dequeue()
  20.             else lock this (fun () -> local_queue.Dequeue())
  21.  
  22.         member this.getSize =
  23.             lock this (fun () -> local_queue.Count)
  24.  
  25.         member this.firstElement =
  26.             lock this (fun () -> local_queue.Peek)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement