review of std.parallelism

dsimcha dsimcha at yahoo.com
Sat Mar 19 12:18:22 PDT 2011


On 3/19/2011 2:25 PM, Michel Fortin wrote:
> On 2011-03-19 14:14:51 -0400, Michel Fortin <michel.fortin at 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.


More information about the Digitalmars-d mailing list