www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to use global variables spanning modules?

reply "Gary Willoughby" <dev kalekold.net> writes:
Yes i know it's horrible but is it possible to use global 
variables spanning modules?

I need to capture a small amount of program data and deal with it 
in termination signal handlers. The data will be captured from 
various classes spread around different modules.

I'm thinking the easiest solution is a couple of global vars and 
then i can access these in the signal handle after the various 
objects update these values during runtime.

Thoughts?
Jun 21 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

is it possible to use global variables spanning modules?<
Why don't you use a __gshared module-global var, and then import it in all the modules where you need it? Bye, bearophile
Jun 21 2013
parent reply "Gary Willoughby" <dev kalekold.net> writes:
On Friday, 21 June 2013 at 16:29:06 UTC, bearophile wrote:
 Gary Willoughby:

is it possible to use global variables spanning modules?<
Why don't you use a __gshared module-global var, and then import it in all the modules where you need it? Bye, bearophile
I've just been looking at that but a little unsure how to use it. Do you import the module that declares the __gshared var or import the var itself?
Jun 21 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 21 June 2013 at 17:47:49 UTC, Gary Willoughby wrote:
 Do you import the module that declares the __gshared var or 
 import the var itself?
Import the module then use the variable. module globals; int a; // or could be __gshared if you don't want it to be thread local module test1; import globals; a = 10; module test2; import globals; assert(a == 10); // ok because we set it in test1
Jun 21 2013
parent "Gary Willoughby" <dev kalekold.net> writes:
That works great thanks all.
Jun 22 2013