shared: Has anyone used it without a lot of pain?
deadalnix via Digitalmars-d
digitalmars-d at puremagic.com
Thu Apr 6 06:52:01 PDT 2017
On Wednesday, 5 April 2017 at 14:01:24 UTC, Guillaume Piolat
wrote:
> Do we have a missed opportunity with shared?
>
Yes we do.
The #1 problem is that it lack a bridge to and from the "normal"
thread local world. there is literally no way to use shared in a
correct way, you always need to bypass part of the language ATM.
The 3 main way data are shared go as follow :
1/ Producer/consumer.
Thread 1 create some object, send it to thread 2 for processing.
This is common in server applications for instance, where a
thread will accept request and then dispatch it to worker threads.
Right now, there are no way to transfers ownership from one
thread to another, so you pretty much got to cast to shared, move
data to the other thread and then cast back to not shared.
(People who followed closely will notice that this is somewhat
entangled with other problem D has such as nogc exception, see
http://forum.dlang.org/post/ikzzvbtvwhqweqlzxytz@forum.dlang.org
).
2/ Actually shared objects with temporal ownership via a mutex.
Once again, this is about ownership. One can get a temporary
ownership of some shared object which one can manipulate as if it
was thread local for the duration a mutex is held.
The current way to do this is to take the mutex and cast away
shared. This is bad as it breaks all type system guarantees.
To be done safely, this is needs to specify what is actually
owned by the object that is protected by the mutex and what
isn't. A phobos facility could, granted ownership was known, take
an object, take the mutex and then allow to access it in a
"scope" manner based on the lifetime of the mutex's lock. the
accessed object can even be const in case of RWlock and it works
beautifully because const is transitive.
3/ Actually shared object providing method which use atomics and
alike to be thread safe.
This use case is actually decently served by shared today, except
the construction of the object in the first place.
More information about the Digitalmars-d
mailing list