[NAME]
ALL.dao.concurrent.threading

[TITLE]
Multithreading Module

[DESCRIPTION]

The multithreading module mt offers methods for creating tasklets and parallelizing some
typical parallelization tasks over standard container types.

 0.1   Starting Tasklet  

The simplest way to start a tasklet is to use the functional method mt.start to run an ex
pression or a block of codes: 
     
   1  var a = 123
   2  var fut = mt.start { a*a }
   3  var fut2 = mt.start {
   4      var sum2 = 0;
   5      for(var i = 1 : 1000 ){
   6          sum += a*a
   7      }
   8      io.writeln( sum2 )
   9  }
     
This functional method is implemented as a code section (code block as in Ruby) method, a
nd can take a block of code as an additional implicit parameter. mt.start returns the cre
ated tasklet in the form of a future value. The created tasklet may be executed by a reus
ed thread from the internal thread pool or by a newly created thread.

 0.2   Future Value  

Future value is a type that represents a value that is available only after a tasklet has
been completed. The future value for the tasklet can be used to perform simple scheduling
such as blocking the current thread indefinitely or a finite amount of time to wait for t
he thread to finish. For example, 
     
   1  fut.wait()
   2  fut2.wait( 0.1 )
     
Its value can be obtained by, 
     
   1  var value = fut.value()
     
which will block the current tasklet and wait for the completion of the tasklet represent
ed by fut.

 0.3   Parallelized Code Block Methods  

However, the simplest way to do parallel programming is to use the parallelized functiona
l methods of mt, which provides the following parallelized functional methods, 
  *  iterate():
     Iterate a predefined number of times, or iterate over a list, map or array,  and exe
     cute the code block on each of the items; 
  *  map()
     Map a list, map or array to another list, map or array, using the values  returned b
     y the code block; 
  *  apply()
     Apply the values returned by the code block to a list, map or array; 
  *  find()
     Find the first item in a list or map the satisfy the condition as  tested by the cod
     e block. 

Examples, 
     
   1  mt.iterate( 10 ) { io.writeln( X ) }
   2  mt.iterate( {1,2,3,4,5,6}, 3 ) { io.writeln( X ) }
     


 0.4   Other Methods  

The mt module also supports a few other methods: 
  *  select( group: list<@T>, timeout = -1.0 ) => tuple<selected: none|@T, value: any, 
     status: enum<selected,timeout,finished>>
     Select on a group of channels or future values, wait with a timeout for data become 
     available from any of the channels. It returns a tuple with the following possible v
     alues:  
       *  (Channel|Future, data, $selected);
       *  (none, none, $timeout);
       *  (none, none, $finished);