Does anyone understand how to use "shared" types with concurrency send/receive functions?
crimaniak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 16 16:15:10 PDT 2017
On Monday, 14 August 2017 at 03:59:48 UTC, Jonathan M Davis wrote:
> The way to handle shared is to protect the section of code
> that's using the shared object with either a mutex or
> synchronized block, and then you cast away shared from the
> object within that section and operate on it as thread-local.
> When you're done, you make sure that you don't have any
> thread-local references to the data, and you release the mutex
> or exit the synchronized block. e.g. something like
>
> shared T sharedObj = getSharedObj();
>
> synchronized(mutex)
> {
> T nonSharedObj = cast(T)sharedObject
>
> // do stuff...
>
> // make sure that no references to nonSharedObj have escaped
> }
Casting objects just to work with it every time is so bad style
for me that I even didn't consider such possibility. In fact, I
did make something like this but with __gshared both object and
mutex. I think I need to review this part of site engine and my
programming habits too.
I wonder if it possible and usable to make some template to
support this pattern, where we give mutex(es), shared object(s)
and delegate to operate with objects as non-shared.
More information about the Digitalmars-d-learn
mailing list