On 7 May 2015 at 20:18, Johannes Pfau via Digitalmars-d <digitalmars-d@puremagic.com> wrote: >> If variable 'alice' and variable 'bob' are both shared, and >> reading from 'bob', then writing to 'alice'; would instructions >> be moved around, so reading from 'bob' could actually occur after >> writing to 'alice' ? > > Not sure about shared (I don't think anybody knows what exactly shared > is supposed to do) but if you use volatileLoad/Store > these instructions won't be moved around.
I've change my mind over the years, so who knows whether or not I'll change again, but this is the current definition I give to it. Shared data on an ABI level is exactly the same as __gshared, but that's where the similarities end. On a semantic level, shared comes with a transitive, and statically type-checked guarantee that either taking it's address or passing it's reference around stays within the bounds of it's given qualifier, so everything it's address passes through must also be shared. To make it an even stronger type, non-shared data can not be implicitly promoted to shared. Of course, explicit casts allow you to demote and promote the 'shared' qualifier as much as you like, circumventing all guarantee. When used properly though, it's properties make it a prime candidate for the foundation of libraries/programs that centre around the use of atomics. However, shared is not to be confused with a thread-safe atomic type. Thread-safety is left to the end-user on such matters of access. Regards Iain.