digitalmars.D.learn - I can share non shared things?
- gdelazzari (13/13) Jun 19 2018 So, let's say I have this code
- Steven Schveighoffer (9/29) Jun 19 2018 Using Thread directly like that, you can override shared when passing
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
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