Safely writing to the same file in parallel foreach loop

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Nov 14 08:16:58 PST 2012


On Wed, Nov 14, 2012 at 04:56:53PM +0100, Joseph Rushton Wakeling wrote:
> Suppose that I've got a foreach loop in which I write output to a file:
> 
>     auto f = File("test.txt", "w"); f.close();  // to start with a blank file
>     foreach(i; iota(0, 100))
>     {
>         f = File("test.txt", "a");
>         f.writeln(i);
>         f.close();
>     }
> 
> I'm guessing it is at least potentially unsafe to parallelize the
> loop without also considering the file interactions:
> 
>     foreach(i; parallel(iota(0, 100), 20))
>     {
>         f = File("test.txt", "a");  // What happens if 2 threads want to
>         f.writeln(i);               // open this file at the same time?
>         f.close();
>     }
> 
> ... so, is there a way that I can ensure that the file appending
> takes place successfully but also safely in each thread?  Let's
> assume that I don't care about the order of writing, only that it
> takes place.

If you're on Posix, you can use file locks to ensure atomic writes to
the file (all threads have to use it though: it's only an advisory lock,
not a mandatory lock): see the manpage for fcntl, look for F_GETLK.


T

-- 
It always amuses me that Windows has a Safe Mode during bootup. Does
that mean that Windows is normally unsafe?


More information about the Digitalmars-d-learn mailing list