Shared - Another Thread

Vijay Nayar madric at gmail.com
Thu Oct 18 16:31:33 UTC 2018


On Wednesday, 17 October 2018 at 21:12:49 UTC, Stefan Koch wrote:
> Hi,
>
> reading the other shared thread  "shared - i need to be 
> useful"(https://forum.dlang.org/thread/mailman.4299.1539629222.29801.digitalmars-d@puremagic.com)
>
> let me to an important realisation concerning the reason 
> shareding data across threads is so unintuitve and hard to get 
> right.
> The reason is that sharing in the real world has nothing to do 
> with using something and the same time.
> For example: If I share my flat with another person, that 
> person, while occupying the same flat as me, cannot actually 
> occupy the same space. It is physically impossible.
>
> In other words sharing does not mean for multiple entities to 
> own something it's rather about diving and managing the 
> (temporary) ownership of fragments.
>
> Therefore if ownership is unclear sharing is impossible.
> The safest default for something shared with unclear ownership 
> is to view it as untouchable/unreadble/unwritable until 
> ownership is established.

My understanding is that the "shared" keyword can be useful 
especially with array types that are operated on by multiple 
threads.  Some algorithms put together data following specific 
rules on how that data can be fragmented.

Imagine a simple algorithm that does logic on very long numbers, 
split into bytes.  One multi-threaded implementation may use 4 
threads.  The first operating on bytes 0, 4, 8, etc.  The second 
operating on bytes 1, 5, 9, etc.

In this case, a mutex or lock isn't actually needed, because the 
algorithm itself assures that threads don't collide.

It's an over-simplification, but I think this is basically what 
the prime-number finding algorithm by Jabari Zakiya is doing.


More information about the Digitalmars-d mailing list