test2.d 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. alias uint8 = ubyte;
  2. import std.stdio;
  3. import std.concurrency : receive, send, spawn, thisTid, Tid;
  4. import core.time : Duration, dur;
  5. import core.thread : Thread;
  6. Tid[4] workers_tids;
  7. struct Msg1{
  8. uint8 command;
  9. }
  10. struct Msg2{
  11. Tid sender;
  12. string client_id;
  13. }
  14. struct Msg21{
  15. uint8 command;
  16. string client_id;
  17. }
  18. struct Msg3{
  19. Tid sender;
  20. uint8 command;
  21. string client_id;
  22. }
  23. void test2_worker(uint8 n){
  24. writeln("started worker = ", n);
  25. while(true){
  26. //Thread.sleep(dur!"seconds"(120));
  27. receive(
  28. (Msg1 m) => writeln("got Msg1: command = ", m.command),
  29. (Msg2 m) => writeln("got Msg2: from ", m.sender, ", client_id = ", m.client_id),
  30. (Msg21 m) => writeln("got Msg21: command = ", m.command, ", client_id = ", m.client_id),
  31. (Msg3 m) => writeln("got Msg3: from ", m.sender, ", command ", m.command)
  32. );
  33. }
  34. writeln("stopped worker = ", n); // unreachable
  35. }
  36. void test2_ticker(){
  37. writeln("started ticker");
  38. while(true){
  39. Thread.sleep(dur!"seconds"(5));
  40. writeln("tick (5 seconds passed)");
  41. }
  42. }
  43. void test2_spawner(){
  44. spawn(&test2_ticker);
  45. uint8 i = 4;
  46. while(i > 0){
  47. workers_tids[i - 1] = spawn(&test2_worker, i);
  48. i--;
  49. }
  50. Thread.sleep(dur!"seconds"(30));
  51. send(workers_tids[0], Msg1(42)); // async command ?
  52. send(workers_tids[1], Msg2(thisTid, "client_123"));
  53. send(workers_tids[2], Msg21(5, "client_456"));
  54. send(workers_tids[3], Msg3(thisTid, 99, "client_789"));
  55. Thread.sleep(dur!"seconds"(20));
  56. writeln("the end");
  57. }
  58. /*
  59. // message passing - async send
  60. > make run
  61. ./vtest2
  62. started worker = 2
  63. started worker = 1
  64. started ticker
  65. started worker = 3
  66. started worker = 4
  67. tick (5 seconds passed)
  68. tick (5 seconds passed)
  69. tick (5 seconds passed)
  70. tick (5 seconds passed)
  71. tick (5 seconds passed)
  72. got Msg1: command = 42
  73. got Msg3: from Tid(7f8a8dcc38f0), command 99
  74. got Msg21: command = 5, client_id = client_456
  75. got Msg2: from Tid(7f8a8dcc38f0), client_id = client_123
  76. tick (5 seconds passed)
  77. tick (5 seconds passed)
  78. tick (5 seconds passed)
  79. tick (5 seconds passed)
  80. the end
  81. tick (5 seconds passed)
  82. tick (5 seconds passed)
  83. tick (5 seconds passed)
  84. tick (5 seconds passed)
  85. tick (5 seconds passed)
  86. tick (5 seconds passed)
  87. tick (5 seconds passed)
  88. tick (5 seconds passed)
  89. tick (5 seconds passed)
  90. tick (5 seconds passed)
  91. tick (5 seconds passed)
  92. tick (5 seconds passed)
  93. tick (5 seconds passed)
  94. tick (5 seconds passed)
  95. tick (5 seconds passed)
  96. hello here!
  97. tick (5 seconds passed)
  98. tick (5 seconds passed)
  99. tick (5 seconds passed)
  100. ...
  101. */