plain text version:threads.csp



  1| package threads;
  2| //|

  3| //| test of 11 threads running simultaneously

  4| //| 10 of them will cause garbage collection at one time

  5| //| and one will run constantly, even during garbage collection

  6| //|

  7| 
  8| var all = new array();      
  9| var all_guard = new mutex();
 10| 
 11| // will cause garbage collection due to string allocations

 12| function thread_func(sign) {
 13|     var i, x = "";
 14|     for (i = 0; i < 1000; ++i) 
 15|     {
 16|         // will be suspended here during gc cycle 

 17|         // somewhere in the next statement

 18|         x += " hello " + " goodbye ";
 19|         // will be awakened after gc cycle

 20|         print(sign);
 21|     }
 22|     print("*****");
 23|     synchronized(all_guard) 
 24|     {  
 25|         all[sign] = new thread(thread_func,sign);
 26|     }
 27| }
 28| 
 29| // will not cause garbage collection. nothing allocating 

 30| // will be active even during gc cycle

 31| function nongc_thread_func() {
 32|     var i,x = 0;
 33|     for (i = 0; i < 1000; ++i) 
 34|     {
 35|        x = x + 1;
 36|        print("_");
 37|     }
 38|     print("/////");
 39|     new thread(nongc_thread_func);
 40| }
 41| 
 42| function main()
 43| {
 44|     var i;
 45|     for (i = 0; i < 10; ++i) all.push(new thread(thread_func,i));
 46|     new thread(nongc_thread_func);
 47|     var w;
 48|     do  
 49|     {
 50|       w = 0;
 51|       synchronized(all_guard) 
 52|       {
 53|         for (i = 0; i < all.length; ++i) 
 54|         {        
 55|           if(all[i].running) w = 1;
 56|         }
 57|       }
 58|       print("-");
 59|     } while(w);
 60| }
 61|