Death by Concurrency

functor
import Application
define
   Args = {Application.getArgs
           record(threads(single type:int optional:false)
                  times(  single type:int optional:false))}

   Start Done

   proc {Main N RestDone}
      if N==0 then RestDone=unit
      else ThisDone in
         thread ID={Thread.this} in
            %% block the thread until the start signal
            {Wait Start}
            %% explicitly yield Args.times times, then finish
            %% on the following iteration
            for _ in 1..Args.times do
               {Thread.preempt ID}
            end
            %% pass on the indication that this thread and
            %% all those created earlier have finished
            ThisDone=RestDone
         end
         %% create remaining threads
         {Main N-1 ThisDone}
      end
   end

   %% create all threads (blocking on Start)
   {Main Args.threads Done}

   if Args.times==0 then
      %% we are just interested in the thread creation overhead
      skip
   else
      %% we are also interested in the thread execution overhead
      %% unblock all threads
      Start=unit
      %% wait until they all finish
      {Wait Done}
   end
   
   {Application.exit 0}
end