[M3devel] missing attachment, sorry

Dragiša Durić dragisha at m3w.org
Fri Apr 20 14:21:56 CEST 2007


-- 
Dragiša Durić <dragisha at m3w.org>
-------------- next part --------------
MODULE TestThreads EXPORTS Main;

IMPORT
  Fmt,
  IO,
  Thread;

TYPE
  MyClosure = Thread.Closure OBJECT
    my,
    next: Thread.Condition;
  OVERRIDES
    apply := JustDoIt;
  END;

VAR
  glob := NEW(MUTEX);
  started: CARDINAL;

PROCEDURE JustDoIt(c: MyClosure): REFANY =
  BEGIN
    LOCK glob DO
      INC(started);
      Thread.Wait(glob, c.my);
      Thread.Signal(c.next);
    END;
    RETURN NIL;
  END JustDoIt;

CONST
  nThreads = 40;
  
VAR
  my, first, a, b: Thread.Condition;
  
BEGIN
  FOR j:=1 TO 10 DO
    IO.Put("Forking " & Fmt.Int(nThreads) & " threads... (" & Fmt.Int(j) & ")\n");
    IF j MOD 10 = 0 THEN
      IO.Put("(" & Fmt.Int(j) & ")");
    END;
    my := NEW(Thread.Condition);
    a:=NEW(Thread.Condition);
    started:=0;
    FOR i:=1 TO nThreads DO
      IF i=nThreads THEN
        b:=my;
      ELSE
        b:=NEW(Thread.Condition);
      END;
      EVAL Thread.Fork(NEW(MyClosure, my := a, next := b));
      IF i=1 THEN
        first:=a;
      END;
      a:=b;
    END;
    IO.Put("Forked.\n");
    IO.Put(",");
    WHILE started<nThreads DO
      Thread.Pause(0.0001d0);
      IO.Put(":");
    END;
    LOCK glob DO
      Thread.Signal(first);
      Thread.Wait(glob, my);
    END;
    IO.Put(" ");
    (*
    IO.Put("All done.\n");
    *)
  END;
END TestThreads.


More information about the M3devel mailing list