Browse Source

test1 add fact

221V 3 weeks ago
parent
commit
66b4d53108
2 changed files with 33 additions and 65 deletions
  1. 1 6
      vtest2/source/app.d
  2. 32 59
      vtest2/source/test1.d

+ 1 - 6
vtest2/source/app.d

@@ -3,7 +3,6 @@ import std.stdio;
 
 /**/
 // test1
-//import std.parallelism : defaultPoolThreads;
 import std.concurrency : spawn;
 import core.time : Duration, dur;
 import core.thread : Thread;
@@ -34,16 +33,12 @@ import test3 : test3_spawner;
 
 
 void main(){
-  //writeln("defaultPoolThreads = ", defaultPoolThreads()); // 19
-  //defaultPoolThreads(8); // changes nothing because all threads works on 1 CPU core, not on multicores..(
-  //writeln("defaultPoolThreads = ", defaultPoolThreads()); // 8
-  
   spawn(&test1_spawner); // test1 run
   //spawn(&test2_spawner); // test2 run
   //spawn(&test3_spawner); // test3 run
   
   //Thread.sleep(dur!"seconds"(125));
-  Thread.sleep(dur!"seconds"(30));
+  //Thread.sleep(dur!"seconds"(3));
   
   writeln("hello here!");
 }

+ 32 - 59
vtest2/source/test1.d

@@ -1,18 +1,42 @@
 
+// todo test   https://dlang.org/phobos/std_parallelism.html
+
+
 alias uint8  = ubyte;
+alias uint64 = ulong;
+
 
 import std.stdio;
-//import std.parallelism : defaultPoolThreads;
-import std.concurrency : receive, receiveOnly, receiveTimeout, send, spawn, thisTid, Tid, ThreadScheduler, FiberScheduler;
+import std.concurrency : spawn;
 import core.time : Duration, dur;
 import core.thread : Thread;
 
 
+
+uint64 fact(uint64 n){
+  return fact(n, 1);
+}
+
+uint64 fact(uint64 n, uint64 a){
+  if(n == 0){
+    return a;
+  }else{
+    return fact(n - 1, n * a);
+  }
+}
+
+
 //void test1_worker(uint8 n){
 void test1_worker(int n){
   writeln("started worker = ", n);
   
-  Thread.sleep(dur!"seconds"(120));
+  //Thread.sleep(dur!"seconds"(120));
+  Thread.sleep(dur!"seconds"(3));
+  
+  auto v120 = fact(120);
+  writeln("worker = ", n, " fact 120 = ", v120);
+  
+  Thread.sleep(dur!"seconds"(3));
   
   writeln("stopped worker = ", n);
 }
@@ -20,76 +44,25 @@ void test1_worker(int n){
 
 void test1_spawner(){
   //uint8 i = 10; // 10 = 1.8 Mb RAM // 255 = 2.9 Mb RAM // 1000 = 6.1 Mb RAM // 10_000 = 47.9 Mb RAM // 100_000 = 180.3 -> 434.4 Mb RAM ,, 161.8 -> 607.7 Mb RAM
-  int i = 100_000; // freeze with 180.3 -> 434.4 Mb RAM .. (( 161.8 -> 607.7 Mb RAM
-  //int i = 1_000;
-  
-  auto scheduler = new ThreadScheduler();
-  //auto scheduler = new FiberScheduler;
-  
-  //scheduler.start({});
-  
-  while(i > 0){
-    scheduler.spawn({
-      writeln("thread ", thisTid(), " == i = ", i);
-      //Thread.sleep(dur!"seconds"(3));
-      Thread.sleep(dur!"seconds"(120));
-    });
-    i--;
-  }
-  
-  
-  /*
-  //while(i > 0){ // 10 CPU' cores ?
-  foreach(i2; 0..10){ // 10 CPU' cores ?
-    //auto i2 = i;
-    scheduler.spawn({
-      foreach(j; 0..10){ // 10 threads per CPU core ?
-        writeln("thread ", thisTid(), " == i, j = ", i2, " ", j);
-        Thread.sleep(dur!"seconds"(3));
-        //Thread.sleep(dur!"seconds"(120));
-      }
-    });
-    //i--;
-  }
-  */
+  int i = 100_000; // fails
   
-  /*
+  /**/
   while(i > 0){
     spawn(&test1_worker, i);
     
     i--;
   }
-  */
+  /**/
   
 }
 
 
 /*
-// 10 threads (10 OS processes ?) - but with problem for 100_000 threads - but only on one CPU core (
 
 > make run
 ./vtest2
-started worker = 8
-started worker = 7
-started worker = 9
-started worker = 6
-started worker = 1
-started worker = 10
-started worker = 5
-started worker = 3
-started worker = 2
-started worker = 4
-stopped worker = 8
-stopped worker = 9
-stopped worker = 7
-stopped worker = 10
-stopped worker = 6
-stopped worker = 1
-stopped worker = 4
-stopped worker = 3
-stopped worker = 5
-stopped worker = 2
-hello here!
+...
+// core.thread.threadbase.ThreadError@core/thread/threadbase.d(1297): Error creating thread
 
 */