www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I can share non shared things?

reply gdelazzari <giacky98.mail gmail.com> writes:
So, let's say I have this code

https://run.dlang.io/is/CaMJjd

It doesn't compile, and that makes sense since myNotSharedObject 
is... not shared. So I can fix it by making the class shared and 
making a shared instance of it, like this

https://run.dlang.io/is/hoMFD1

And that's how shared is supposed to work, right? Cool, I like it.

But then I try this and... it compiles and runs?

https://run.dlang.io/is/mNQsH1

Or, even worse, even this compiles and runs 
https://run.dlang.io/is/oyzHcw

So, if I can share non-shared variables/objects/whatever, then 
what's the point of shared?
Jun 19 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/18 4:09 PM, gdelazzari wrote:
 So, let's say I have this code
 
 https://run.dlang.io/is/CaMJjd
 
 It doesn't compile, and that makes sense since myNotSharedObject is... 
 not shared. So I can fix it by making the class shared and making a 
 shared instance of it, like this
 
 https://run.dlang.io/is/hoMFD1
 
 And that's how shared is supposed to work, right? Cool, I like it.
 
 But then I try this and... it compiles and runs?
 
 https://run.dlang.io/is/mNQsH1
 
 Or, even worse, even this compiles and runs https://run.dlang.io/is/oyzHcw
 
 So, if I can share non-shared variables/objects/whatever, then what's 
 the point of shared?
Using Thread directly like that, you can override shared when passing data around. It's because natively, pthreads (or whatever threading system) doesn't care about shared, so it's on you to make sure you use it correctly. Note that before TDPL, shared was this "new undefined" concept, even in D2, and Thread existed in D1 where there was no shared concept. If you want full shared guards, use std.concurrency only. -Steve
Jun 19 2018