On 3/19/2011 2:25 PM, Michel Fortin wrote:
On 2011-03-19 14:14:51 -0400, Michel Fortin <michel.for...@michelf.com>
said:

I'm not too convinced about the "I know what I'm doing" argument when
I look at this example from asyncBuf's documentation:

auto lines = File("foo.txt").byLine();
auto duped = map!"a.idup"(lines); // Necessary b/c byLine() recycles
buffer

// Fetch more lines in the background while we process the lines already
// read into memory into a matrix of doubles.
double[][] matrix;
auto asyncReader = taskPool.asyncBuf(duped);

foreach(line; asyncReader) {
auto ls = line.split("\t");
matrix ~= to!(double[])(ls);
}

Look at the last line of the foreach. You are appending to a
non-shared array from many different threads. How is that not a race
condition?

.... or maybe I just totally misunderstood asyncBuf. Rereading the
documentation I'm under the impression I'd have to write this to get
what I expected:

foreach (line; parallel(asyncReader))
...

And that would cause a race condition. If that's the case, the example
is fine. Sorry for the misunderstanding.


Right. And this is pretty obviously a race. The other example (without the parallel) is completely safe.

Reply via email to