shared - i need it to be useful

Simen Kjærås simen.kjaras at gmail.com
Sun Oct 21 15:18:05 UTC 2018


On Sunday, 21 October 2018 at 13:24:49 UTC, Stanislav Blinov 
wrote:
> On Sunday, 21 October 2018 at 11:25:16 UTC, aliak wrote:
>> When I say ok, I mean assuming the implementer actually wrote 
>> correct code. This applies to any shared method today as well.
>
> This ("ok") can only be achieved if the "implementor" (the 
> "expert") writes every function self-contained, at which point 
> sharing something from user code becomes a non-issue (i.e. it 
> becomes unnecessary). But that's not a very useful API. As soon 
> as you have more than one function operating on the same data, 
> the onus is on the user (the caller) to call those functions in 
> correct order, or, more generally, without invalidating the 
> state of shared data.

The onus is *always* on the user to write function calls in the 
correct order, multi-threading or not. We expect programmers to 
be able to figure out why this doesn't print 'Hello world!':

void main() {
     import std.stdio;
     string hello;
     writeln(hello);
     hello = "Hello world!";
}

We also expect writeln to be written in such a way that it 
doesn't corrupt random data or cause life-threatening situations 
just because hello was uninitialized upon calling writeln, and 
assigned afterwards. We should expect the same of multi-threaded 
programs.

This places the onus of writing thread-safe code on the writer of 
the multi-threaded equivalent of writeln and string.opAssign. 
Only this way can the user of the library write code and not 
expect things to blow up in their face.

--
   Simen


More information about the Digitalmars-d mailing list