<div dir="ltr"><div dir="ltr">On Tue, Aug 4, 2020 at 5:40 PM Sebastiaan Koppe via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Monday, 3 August 2020 at 21:56:34 UTC, Manu wrote:<br>
> Shared recently received a `-preview` which makes it really <br>
> mean something;<br>
> this is what shared means:<br>
> 1. The data is shared; therefore, it is invalid to read or <br>
> write that<br>
> memory.<br>
> 2. The reason this is useful as an attribute, is because you <br>
> are able to<br>
> attribute methods. Ability to author a set of threadsafe <br>
> methods and<br>
> clearly distinguish them from non-threadsafe methods is a <br>
> powerful<br>
> advantage over C++.<br>
<br>
For some reason I often end up with multiple threads and the <br>
coordination that comes with it. Shared has been very helpful for <br>
me and I am using no. 2 with good success.<br>
<br>
There is just one thing about shared I don't understand. If I <br>
design my object such that the non-shared methods are to be used <br>
thread-local and the shared methods from any thread, it follows <br>
that I should be able to call said shared methods from both a <br>
shared and non-shared instance of that object.<br>
<br>
Often I workaround it be introducing a non shared method that <br>
forwards to the shared method by means of casting.<br></blockquote><div><br></div><div>Yes, this is a thing I talked at great length 1-2 years ago.</div><div>If you take shared to mean "is thread-safe", then my idea was that not-shared -> shared implicit conversion should be possible.</div><div>What I often do is this:</div><div><br></div><div>struct Thing</div><div>{</div><div>  ref shared(Thing) implSharedCast() { return *cast(shared)&this; }</div><div>  alias implSharedCast this;</div><div>}</div><div><br></div><div>If that were an implicit conversion, that implies a slight change of meaning of shared (to one that I consider immensely more useful), but it's more challenging for the compiler to prove with confidence, and there's a lot of resistance to this change.</div><div>In the mean-time, until the shared usage patterns are more well-proven, I recommend you try to use the 'alias this' pattern I show above, and report on any issues you encounter using this scheme. If no issues are identified with extensive real-world usage data, I will push again for that implicit conversion rule.</div></div></div>