File: main_pth.sml

package info (click to toggle)
smlsharp 4.2.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 125,348 kB
  • sloc: ansic: 16,737; sh: 4,347; makefile: 2,228; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (42 lines) | stat: -rw-r--r-- 952 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
val args = CommandLine.arguments ()
val nthreads = case args of s::_ => valOf (Int.fromString s) | _ => 1

val incr = _import "incr" : () -> int

fun get () =
    let
      val n = incr ()
    in
      if n >= Vector.length files
      then NONE
      else SOME (Vector.sub (files, n))
    end

fun task a =
    case get () of
      NONE => a
    | SOME s => (parse s; task (a + 1))

fun spawn n =
    let
      val a = n div 2
      val b = n - a - 1
      val t1 =
          if a > 0
          then SOME (Pthread.Thread.create (fn _ => spawn a))
          else NONE
      val t2 =
          if b > 0
          then SOME (Pthread.Thread.create (fn _ => spawn b))
          else NONE
      val m = task 0
      val r1 = case t1 of SOME t => Pthread.Thread.join t | NONE => 0
      val r2 = case t2 of SOME t => Pthread.Thread.join t | NONE => 0
    in
      r1 + r2 + m
    end

fun main () =
    print (Int.toString (spawn nthreads))

val _ = main ()