A little of coordination for Rosettacode

bearophile bearophileHUGS at lycos.com
Tue Mar 26 16:20:00 PDT 2013


This task has just a Tango entry:
http://rosettacode.org/wiki/Concurrent_computing


So I am writing a Phobos version. This seems to work as requested:


import std.stdio, std.random, std.parallelism, core.thread, 
core.time;

void main() {
     foreach (m; ["Enjoy", "Rosetta", "Code"])
         task!((s) {
             Thread.sleep(uniform(0, 1000).dur!"msecs");
             s.writeln;
         })(m).executeInNewThread;
}



I have also tried with a parallel foreach, the syntax is cleaner, 
but to me it always print the strings in the given order (so it's 
not doing what the task requires), do you know why?


import std.stdio, std.random, std.parallelism, core.thread, 
core.time;

void main() {
     foreach (s; ["Enjoy", "Rosetta", "Code"].parallel(1)) {
         Thread.sleep(uniform(0, 1000).dur!"msecs"); // Can't use 
UFCS.
         s.writeln;
     }
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list