221V 3 weeks ago
parent
commit
65679e13ef
3 changed files with 89 additions and 6 deletions
  1. 10 3
      vtest2/source/app.d
  2. 0 3
      vtest2/source/test1.d
  3. 79 0
      vtest2/source/test4.d

+ 10 - 3
vtest2/source/app.d

@@ -1,14 +1,14 @@
 
 import std.stdio;
 
-/**/
+/*
 // test1
 import std.concurrency : spawn;
 import core.time : Duration, dur;
 import core.thread : Thread;
 
 import test1 : test1_spawner;
-/**/
+*/
 
 
 /*
@@ -32,10 +32,17 @@ import test3 : test3_spawner;
 */
 
 
+// test4
+import std.concurrency : spawn;
+
+import test4 : test4_spawner;
+
+
 void main(){
-  spawn(&test1_spawner); // test1 run
+  //spawn(&test1_spawner); // test1 run
   //spawn(&test2_spawner); // test2 run
   //spawn(&test3_spawner); // test3 run
+  spawn(&test4_spawner); // test4 run
   
   //Thread.sleep(dur!"seconds"(125));
   //Thread.sleep(dur!"seconds"(3));

+ 0 - 3
vtest2/source/test1.d

@@ -1,8 +1,5 @@
 
-// todo test   https://dlang.org/phobos/std_parallelism.html
 // https://git.4dev.win/221V/vibed_test2/src/4d2bd29ff6d5199507146a14f0ce07d863c776f2/vtest2/source/app.d
-// https://chat.qwen.ai/c/5cfd2bc3-66eb-4ca5-a2cd-7126e99e4dc4
-// https://vibed.org/api/vibe.core.channel/
 
 alias uint8  = ubyte;
 alias uint64 = ulong;

+ 79 - 0
vtest2/source/test4.d

@@ -0,0 +1,79 @@
+
+// https://dlang.org/phobos/std_parallelism.html
+
+alias uint8  = ubyte;
+alias uint64 = ulong;
+
+
+import std.stdio;
+import std.concurrency : spawn;
+import std.parallelism : parallel;
+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 test4_worker(uint8 n){
+void test4_worker(int n){
+  writeln("started worker = ", n);
+  
+  while(true){
+    //Thread.sleep(dur!"seconds"(120));
+    
+    //Thread.sleep(dur!"seconds"(3));
+    
+    auto v20 = fact(20);
+    //writeln("worker = ", n, " fact 20 = ", v20); // this is sync lock ((
+  }
+  
+  Thread.sleep(dur!"seconds"(3));
+  
+  writeln("stopped worker = ", n);
+}
+
+
+void test4_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; // fails
+  //int i = 30_000;
+  //int i = 35_000; // fails
+  
+  /*
+  while(i > 0){
+    spawn(&test1_worker, i);
+    
+    i--;
+  }
+  */
+  
+  int[] ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+  
+  foreach(i, ref num; parallel(ints)){
+    test4_worker(num);
+  }
+  
+}
+
+
+/*
+
+> make run
+./vtest2
+// 21 threads
+// writeln locking
+
+*/
+