Cleaner way of working with a shared resource?

Chris M. chrismohrfeld at comcast.net
Thu Mar 29 18:50:39 UTC 2018


On Thursday, 29 March 2018 at 18:07:50 UTC, Jonathan M Davis 
wrote:
> On Thursday, March 29, 2018 17:41:15 Chris M. via 
> Digitalmars-d-learn wrote:
>> [...]
>
> In general, the correct way to deal with a shared object is to 
> protect access to it with a mutex and then within that 
> protected section, you cast away shared so that it's treated as 
> thread-local so that it can be operated on. Then when you 
> release the mutex, you make sure that no thread-local 
> references to the shared object persist. Most operations really 
> aren't supposed to work while the object is treated as shared, 
> because then you have a threading problem if the operation is 
> non-atomic and doesn't protect itself with a mutex (though a 
> member function on an object certainly could use a mutex 
> internally, allowing it to be shared and work on a shared 
> object).
>
> [...]

Awesome, thanks for the explanation. Didn't realize synchronized 
had that extra syntax and what it did, that'll save me a few 
headaches. Now what would really be nice is if that syntax also 
cast away the shared implicitly while inside the critical 
section, but I can deal with it for now.


More information about the Digitalmars-d-learn mailing list