On 27-3-2013 0:20, bearophile wrote:
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;
}
}
Output on my system:
C:\test
Rosetta
Enjoy
Code
C:\test
Code
Enjoy
Rosetta
C:\test
Enjoy
Rosetta
Code
C:\test
Enjoy
Code
Rosetta
C:\test
Code
Enjoy
Rosetta