    [NAME]
    ALL.misc.comparison.channel

    [TITLE]
    Communication Channel (Go)

    [DESCRIPTION]

    The main differences between Go channel and Dao channel are:
      *  Go has syntax support for channels; But Dao doesn't;
      *  Dao channel has builtin support for timeout for both sending and receiving; But Go
         channel doesn't;
      *  Data receive from Dao channel has an additional attribute to indicate whether the
         channel has been closed; But Go channel doesn't support this;

       1  # Go
       2
       3  package main
       4
       5  const N = 1000000;
       6
       7  var c1 = make(chan int, 1)
       8  var c2 = make(chan int, 1)
       9
      10  func Producer( ch chan int ){
      11      for index := 0; index < N; index++ {
      12          if index % 100000 == 0 { println( "sending", index ) }
      13          ch <- index
      14      }
      15      c1 <- 1
      16  }
      17
      18  func Consumer( ch chan int ){
      19      println( "Consumer" )
      20      for index := 0; index < N; index++ {
      21          var data = <-ch
      22          if int(data) % 100000 == 0 { println( "received", data ) }
      23      }
      24      c2 <- 1
      25  }
      26
      27  func main() {
      28      ch := make(chan int, 1)
      29
      30      go Producer( ch )
      31      go Consumer( ch )
      32      <-c1
      33      <-c2
      34  }

       1  # Dao
       2
       3  const N = 1000000
       4
       5  routine Producer( chan : mt::channel<int> )
       6  {
       7      index = 0;
       8      while( ++index <= N ){
       9          if( index % 10000 == 0 ) io.writeln( "sending", index )
      10          chan.send( index )  # Or chan.send( index, 1.5 ) for a 1.5 second timeout;
      11      }
      12      chan.cap(0)  # set channel buffer size to zero to close the channel;
      13  }
      14
      15  routine Consumer( chan : mt::channel<int> )
      16  {
      17      while(1){
      18          data = chan.receive()  # Or chan.receive( 1.5 ) for a 1.5 second timeout;
      19          if( (int)data.data % 10000 == 0 ) io.writeln( "received", data );
      20          if( data.status == $finished ) break
      21      }
      22  }
      23
      24  routine main()
      25  {
      26      chan = mt::channel<int>(1)
      27
      28      Producer( chan ) !!
      29      Consumer( chan ) !!
      30
      31      # Tasklets are automatically joined at the end of the program;
      32  }