June 19, 2018 I can share non shared things? | ||||
---|---|---|---|---|
| ||||
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? |
June 19, 2018 Re: I can share non shared things? | ||||
---|---|---|---|---|
| ||||
Posted in reply to gdelazzari | 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
|
Copyright © 1999-2021 by the D Language Foundation