test1.d 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // todo test https://dlang.org/phobos/std_parallelism.html
  2. alias uint8 = ubyte;
  3. alias uint64 = ulong;
  4. import std.stdio;
  5. import std.concurrency : spawn;
  6. import core.time : Duration, dur;
  7. import core.thread : Thread;
  8. uint64 fact(uint64 n){
  9. return fact(n, 1);
  10. }
  11. uint64 fact(uint64 n, uint64 a){
  12. if(n == 0){
  13. return a;
  14. }else{
  15. return fact(n - 1, n * a);
  16. }
  17. }
  18. //void test1_worker(uint8 n){
  19. void test1_worker(int n){
  20. writeln("started worker = ", n);
  21. //Thread.sleep(dur!"seconds"(120));
  22. Thread.sleep(dur!"seconds"(3));
  23. auto v120 = fact(120);
  24. writeln("worker = ", n, " fact 120 = ", v120);
  25. Thread.sleep(dur!"seconds"(3));
  26. writeln("stopped worker = ", n);
  27. }
  28. void test1_spawner(){
  29. //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
  30. int i = 100_000; // fails
  31. /**/
  32. while(i > 0){
  33. spawn(&test1_worker, i);
  34. i--;
  35. }
  36. /**/
  37. }
  38. /*
  39. > make run
  40. ./vtest2
  41. ...
  42. // core.thread.threadbase.ThreadError@core/thread/threadbase.d(1297): Error creating thread
  43. */