Advertisement
Guest User

Untitled

a guest
May 2nd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.18 KB | None | 0 0
  1. open System
  2. open System.Threading
  3.  
  4. let searchMaxInRange (arr:int[]) l r :int =
  5.   let mutable res = 0
  6.   if r>=arr.Length then for x=l to arr.Length-1 do
  7.     if arr.[x]>res then res <- arr.[x]
  8.   else do
  9.      for x=l to r do
  10.      if arr.[x]>res then res <- arr.[x]
  11.   res
  12.  
  13. let searchMax (arr:int[], threadNumber:int) =
  14.   let length = arr.Length
  15.   let res = ref 0
  16.   let step = length / threadNumber + 1
  17.   let threadArray = Array.init threadNumber (fun i ->
  18.       new Thread(ThreadStart(fun _ ->
  19.           Monitor.Enter(arr)
  20.           let threadRes = searchMaxInRange arr (i * step) ((i+1) * step - 1)
  21.           if (threadRes>res.Value) then res := threadRes
  22.           Monitor.Exit(arr)
  23.         ))
  24.     )
  25.   for t in threadArray do
  26.     t.Start()
  27.   for t in threadArray do
  28.     t.Join()
  29.   printfn "%A" res.Value
  30. 0
  31.  
  32. [<EntryPoint>]
  33. let main argv =
  34.     0 // return an integer exit code
  35.  
  36.  
  37. //[<TestCase ([|112;113;11;55;22;111;10;1|], 5, Result = 113)>]
  38. //[<TestCase ([|111110;0;1;0;25;0;1111111111;|], 2, Result = 1111111111)>]
  39. //[<TestCase ([|0;0;0;25;0|], 2, Result = 25)>]
  40. //[<TestCase ([||], 2, Result = 0)>]
  41. //let ``Test`` (arr : int[], t : int) =
  42.   //searchMax (arr, t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement