[NAME]
ALL.dao.concurrent.threading

[TITLE]
Multithreading Module

[DESCRIPTION]

The multithreading module mt offers methods for creating tasklets and synchronization 
constructs such as mutex, condition variable, semaphore and as well as channels. This
module also provides a list of parallelized methods that can make some typical
parallelization tasks straightforward.

 0.1   Starting Tasklet  

The simplest way to start a tasklet is to use the functional method mt.start to run an 
expression or a block of codes:
     
   1  a = 123
   2  fut = mt.start { a*a }
   3  fut2 = mt.start {
   4      for( i = 1 : 1000 ) b = a*a
   5  }
     
This functional method is implemented as a code section (code block as in Ruby) method, 
and can take a block of code as an additional implicit parameter. mt.start returns the 
created tasklet in the form of a future value. The created tasklet may be executed by a
reused 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 
the thread to finish. For example,
     
   1  fut.wait()
   2  fut2.wait( 0.1 )
     
Its value can be obtained by,
     
   1  value = fut.value()
     
which will block the current tasklet and wait for the completion of the tasklet
represented by fut.

 0.3   Using Parallelized Code Block Methods  

However, the simplest way to do parallel programming is to use the parallelized
functional 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
     execute 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 by
     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 code
     block.

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