how does 'shared' affect member variables?

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 9 12:59:57 PDT 2015


On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:
> What does 'shared' do to member variables?
>
> It makes sense to me to put it on a global variable, but what 
> sense does it make putting it on a member of a class? What 
> happens if you try to access a member of a class/struct 
> instance from another thread that is not marked 'shared'?
>
> Also, I wasn't able to find any thorough documentation on 
> shared, so if someone has a link, that would be helpful.
>
>  Thanks
>    Bit

If a variable/class/struct etc is not shared, for variables and 
struct, you find their initial value. For class, you get null.

For first timers (I started using shared keyword more than 2 
years ago), do not forget that: a shared method is all about 
saying that this method is defined for shared object. So, do not 
get confused. It happened to me a lot.

Bad part of shared is that, you will be repeating it again, and 
again, and again, and again, everywhere. So, try to be patient if 
you are going to be using it for a long time.

Stupidly, shared variables' value cannot be increased/decreased 
directly. Compiler says it is deprecated, and tells me to use 
core.atomic.atomicop. You will see this as well. Hey compiler! I 
know 100% that no other thing will be touching this variable.


More information about the Digitalmars-d-learn mailing list