fork download
  1. import std.stdio, std.concurrency;
  2.  
  3. void foo()
  4. {
  5. bool cont = true;
  6.  
  7. while (cont)
  8. {
  9. receive( // delegates are used to match the message type
  10. (int msg) => writeln("int received: ", msg),
  11. (Tid sender) { cont = false; sender.send(-1); },
  12. (Variant v) => writeln("huh?") // Variant matches any type
  13. );
  14. }
  15. }
  16.  
  17. void main()
  18. {
  19. auto tid = spawn(&foo); // spawn a new thread running foo()
  20.  
  21. foreach (i; 0 .. 10)
  22. tid.send(i); // send some integers
  23. tid.send(1.0f); // send a float
  24. tid.send("hello"); // send a string
  25. tid.send(thisTid); // send a struct (Tid)
  26.  
  27. receive((int x) => writeln("Main thread received message: ", x));
  28. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.d(10): found 'msg' when expecting '.' following 'int'
prog.d(10): found ')' when expecting identifier following 'int.'
prog.d(10): expression expected, not '>'
prog.d(10): found 'writeln' when expecting ')'
prog.d(12): found 'v' when expecting ')'
prog.d(12): expression expected, not '>'
prog.d(12): found 'writeln' when expecting ';' following 'statement'
prog.d(13): found ')' when expecting ';' following 'statement'
prog.d(27): found 'x' when expecting '.' following 'int'
prog.d(27): found ')' when expecting identifier following 'int.'
prog.d(27): expression expected, not '>'
prog.d(27): found 'writeln' when expecting ')'
stdout
Standard output is empty