DIP 1024--Shared Atomics--Community Review Round 1

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Oct 13 04:09:26 UTC 2019

On Saturday, October 12, 2019 9:41:00 PM MDT Ola Fosheim Grøstad via 
Digitalmars-d wrote:
> > It's like I said above; shared MUST be in the language, because
> > it defines one of the fundamental language semantics;
> > thread-local by default.
> Does the shared marker have any effect on generated code? If not,
> then it is just a way to bypass (break) the type system on a
> syntactical level.

The compiler is free to assume that anything that isn't shared is
thread-local and optimize code accordingly. Also, it _does_ matter on some
level with how memory is laid out, because thread-local storage gets used
(IIRC, there were problems on older versions of OS X, because we had to fake
the TLS, because OS X didn't provide proper TLS). Module-level and static
variables which are shared only get initialized once, whereas module-level
and static variables which are thread-local get initialized once per thread,
because each thread gets their own copy. So, while the vast majority of D
objects are thread-local, the distinction between what's thread-local and
what's shared is very much built into how D functions.

That's part of why it's so risky to cast away shared, and why it needs to be
@system. If the programmer screws up that code and allows any shared data to
be treated as thread-local when any other thread could be operating on that
data, then not only does that incur all of the typical issues that come with
screwing up synchronizing data access, but the compiler could make things
worse, because it generated code based on the assumption that the data being
operated on was thread-local.

Fortunately, as long as shared disallows non-atomic, read/write operations
the code that needs to be examined for threading issues is restricted to
@system/@trusted code that involves shared, so the amount of code that the
programmer has to examine and verify to ensure that shared data doesn't end
up being treated as thread-local when multiple threads can operate on it is
limited and should be fairly easy to find.

- Jonathan M Davis

More information about the Digitalmars-d mailing list