associative array with Parallel

Steven Schveighoffer schveiguy at gmail.com
Thu Jul 22 16:39:45 UTC 2021


On 7/22/21 1:46 AM, seany wrote:
> Consider :
> 
>      int [] ii;
>      foreach(i,dummy; parallel(somearray)) {
>        ii ~= somefunc(dummy);
>      }
> 
> 
> This is not safe, because all threads are accessing the same array and 
> trying to add values and leading to collision.

Correct. You must synchronize on ii.

> 
> 
> But :
> 
>      int [] ii;
>      ii.length = somearray.length;
>      foreach(i,dummy; parallel(somearray)) {
>        ii[i] ~= somefunc(dummy);
>      }
> 
> 
> This is safe. In this case, threads are accessing an unique memory 
> location each.

This isn't valid code, because you can't append to an integer. Though I 
think I know what you meant. Is it thread-safe (assuming the array 
elements are appendable)? I think so, but I'd have to see a working example.

> 
> But what about this :
> 
>      int [ string ] ii;
>      ii.length = somearray.length;
>      foreach(i,dummy; parallel(somearray)) {
>        string j = generateUniqueString(i);
>        ii[j] ~= somefunc(dummy);
>      }
> 
> 
> Is this also guaranteed thread safe?

First, this also isn't valid code. You can't set the length of an AA. 
But I'm assuming that length setting is really a placeholder for 
initialization (in your real code). Also, again, you cannot append to an 
integer.

Second, as long as you don't modify the AA *structure*, you can parallel 
with it. In this case, you are generating some string, and appending to 
that. I don't know what your `generateUniqueString` is doing, nor do I 
know what's actually stored as keys in the AA as your initialization 
code is hidden.

If every `j` is guaranteed to already exist as a key in the AA, and the 
code is made to be valid, then I think it is thread-safe.

If any access with a key `j` is inserting a new AA bucket, it is *not* 
thread-safe.

However, this is a tall order, and highly depends on your code. The 
compiler cannot help you here.

> In my 5 runs, I did not see any problems, but I'd like to confirm. Thank 
> you.

Testing 5 times is not a substitute for proving the thread safety.

I have learned one thing long ago about threads and race conditions. 
Just don't do it. Ever. Even if you test 10000 times, and it doesn't 
fail, it will eventually. I've had code that hit a race condition after 
2 weeks of running flat-out. Was one of the hardest things I ever had to 
debug.

-Steve


More information about the Digitalmars-d-learn mailing list