Thread overview
Is it possible to use global variables spanning modules?
Jun 21, 2013
Gary Willoughby
Jun 21, 2013
bearophile
Jun 21, 2013
Gary Willoughby
Jun 21, 2013
Adam D. Ruppe
Jun 22, 2013
Gary Willoughby
June 21, 2013
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?
June 21, 2013
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
June 21, 2013
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?
June 21, 2013
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
June 22, 2013
That works great thanks all.