shared - i need it to be useful

Manu turkeyman at gmail.com
Wed Oct 17 19:25:33 UTC 2018


On Wed, Oct 17, 2018 at 12:05 PM Stanislav Blinov via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On Wednesday, 17 October 2018 at 18:46:18 UTC, Manu wrote:
>
> > I've said this a bunch of times, there are 2 rules:
> > 1. shared inhibits read and write access to members
> > 2. `shared` methods must be threadsafe
> >
> >>From there, shared becomes interesting and useful.
>
> Oh God...
>
> void atomicInc(shared int* i) { /* ... */ }
>
> Now what? There are no "methods" for ints, only UFCS. Those
> functions can be as safe as you like, but if you allow implicit
> promotion of int* to shared int*, you *allow implicit races*.

This function is effectively an intrinsic. It's unsafe by definition.
It's a tool for implementing threadsafe machinery.
No user can just start doing atomic operations on random ints and say
"it's threadsafe", you must encapsulate the threadsafe functionality
into some sort of object that aggregates all concerns and presents an
intellectually sound api.


Let me try one:

void free(void*) { ... }

Now what? I might have dangling pointers... it's a catastrophe!
It's essentially the same argument.
This isn't a function that professes to do something that people might
misunderstand and try to use in an unsafe way, it's a low-level
implementation device, which is used to build larger *useful*
constructs.


More information about the Digitalmars-d mailing list