Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- open System.Threading
- let searchMaxInRange (arr:int[]) l r :int =
- let mutable res = 0
- if r>=arr.Length then for x=l to arr.Length-1 do
- if arr.[x]>res then res <- arr.[x]
- else do
- for x=l to r do
- if arr.[x]>res then res <- arr.[x]
- res
- let searchMax (arr:int[], threadNumber:int) =
- let length = arr.Length
- let res = ref 0
- let step = length / threadNumber + 1
- let threadArray = Array.init threadNumber (fun i ->
- new Thread(ThreadStart(fun _ ->
- Monitor.Enter(arr)
- let threadRes = searchMaxInRange arr (i * step) ((i+1) * step - 1)
- if (threadRes>res.Value) then res := threadRes
- Monitor.Exit(arr)
- ))
- )
- for t in threadArray do
- t.Start()
- for t in threadArray do
- t.Join()
- printfn "%A" res.Value
- 0
- [<EntryPoint>]
- let main argv =
- 0 // return an integer exit code
- //[<TestCase ([|112;113;11;55;22;111;10;1|], 5, Result = 113)>]
- //[<TestCase ([|111110;0;1;0;25;0;1111111111;|], 2, Result = 1111111111)>]
- //[<TestCase ([|0;0;0;25;0|], 2, Result = 25)>]
- //[<TestCase ([||], 2, Result = 0)>]
- //let ``Test`` (arr : int[], t : int) =
- //searchMax (arr, t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement