Prime sieve language race

Petar Petar
Thu Jul 15 17:17:53 UTC 2021


On Thursday, 15 July 2021 at 13:10:44 UTC, Sebastiaan Koppe wrote:
>
> But does that all imply that we have to make all non-threadsafe 
> functions @system? How can we every be @safe?

TL;DR of my previous post is "no" as the answer to your question. 
It's perfectly fine to develop/use `@safe` non-`shared` code. 
That should actually be the default for most projects. You should 
mark code as `@system` only if it implicitly uses shared data (no 
matter if it is actually marked as `shared`) without internal 
synchronization. In the case of [`trustedStdout`][0] it really 
should be `@system` as from the outside (it's signature) it looks 
like it gives access to a thread-local object, but it's actually 
returning an implicitly-shared one (meaning it has shared global 
mutable state, even though it's not marked as `shared`).

Another TL;DR:

* `@safe shared` -> thread-safe
* `@safe` and not `shared` -> safe in single-threaded code; 
unusable if `shared`, unless externally synchronized correctly
* `@system` and not `shared` -> beware! Tricky to use safely even 
with external synchronization, as there could be other code that 
uses this without any synchronization outside of your control

[0]: 
https://github.com/dlang/phobos/blob/v2.097.0/std/stdio.d#L4136


More information about the Digitalmars-d mailing list