Shared
Dominikus Dittes Scherkl
dominikus at scherkl.de
Mon May 13 15:52:02 UTC 2019
On Saturday, 11 May 2019 at 15:24:19 UTC, Jonathan M Davis wrote:
> All that really would do is add a bit of extra syntax around
> locking a mutex and casting away shared.
No, it makes it possible to use mutex in safe functions without
needing to cast and which the compiler CAN guarantee to really be
safe.
> It doesn't really change anything with regards to what happens.
correct. It's a safe wrapper that actually make shared usefull in
one way.
> Also, mutexes aren't the only concurrency mechanism. You have
> to deal with stuff like condition variables and semaphores. And
> even with mutexes, the actual code flow may not be as simple as
>
> * lock mutex
> * cast away shared
No, that's exactly NOT necessary anymore, because the compiler
can deal with this in a safe and checked manner for this special
case.
> * do stuff as thread-local
> * make sure no thread-local references to the data exist
> * unlock mutex
Of course. But that would still be possible if you are willing to
cast and deal with trusted functions. But it would not be
required anymore for the 99% of usecases where simple mutex would
be enough.
>
> That's the basic case and what your suggestion targets, but if
> your code flow needs to be even slightly different, it won't
> work, whereas casting gives a lot more freedom. It might make
> sense to add such a helper on top of casting, but casting is
> still needed.
Yes and it would be far from my opinion to suggest otherwise.
Casting is always possible and need be in a systems language.
But its not safe, and shared should be possible to be used in at
least one way without getting into unsafe territory, I think.
>
> It may ultimately make sense to add helper stuff that hides
> casts, but we really need to figure out the details of how
> shared really works before looking at adding more user-friendly
> helpers on top of it.
And that I think is simple: forbid access without cast (outside
locked blocks). Its the best (and most KISS) idea that has come
up so far - and adding locked blocks seems logical to me, to
allow one safe way to use shared.
More information about the Digitalmars-d
mailing list