Sharing in D

Sean Kelly sean at invisibleduck.org
Mon Aug 11 13:21:32 PDT 2008


== Quote from dsimcha (dsimcha at yahoo.com)'s article
> One thing that recently popped into my mind about the shared/unshared discussion:
>  What about arrays, etc that are manipulated by multiple threads, but in such a
> way that each thread is guaranteed not to touch the same elements as any other
> thread, and to never reallocate the array?  This is something I actually do, for
> such things as filling in large matrices.  If I understand this proposal
> correctly, if the array is unshared, multiple threads can't touch it.  If it is
> shared, the compiler will automatically make it synchronized.  In this case, the
> synchronization overhead might be large and unnecessary.  If I have each thread
> write to separate, unshared data structures and merge them together later, then
> I'm adding needless complexity to my code.

For arrays with elements smaller than the bus width or for unaligned arrays there
are still word tearing issues, which makes such an approach risky at best.  Less
importantly, depending on the size and alignment of the array the threads could
end up all competing for the same cache line, which would render parallelization
of the algorithm largely pointless unless the computation is extremely expensive.
It's almost always better to have each thread work on a copy of the data and then
merge it later.


Sean



More information about the Digitalmars-d mailing list