Safely writing to the same file in parallel foreach loop
Jonathan M Davis
jmdavisProg at gmx.com
Wed Nov 14 13:17:21 PST 2012
On Wednesday, November 14, 2012 18:59:29 Joseph Rushton Wakeling wrote:
> On 11/14/2012 06:49 PM, Vijay Nayar wrote:
> > Could you put the file access in a synchronized block?
> >
> > http://dlang.org/statement.html#SynchronizedStatement
>
> Oh, good call -- seems to work.
I would point out though that given how expensive disk writes are, unless
you're doing a lot of work within the parallel foreach loop, there's a good
chance that it would be more efficient to use std.concurrency and pass the
writes to another thread to do the writing. And the loop itself should still
be able to be a parallel foreach, so you wouldn't have to change much
otherwise. But with the synchronized block, you'll probably end up with each
thread spending a lot of its time waiting on the lock, which will end up
making the whole thing effectively single-threaded. If the work being done in
the parallel foreach is small enough, it might even be the case that simply
making it a normal foreach and ditching the synchronized block would be
faster. But you'll obviously have to experiment to see what works best with
whatever you're doing.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list