Global runtime strings help

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 23 13:33:30 PDT 2011


On Friday, September 23, 2011 13:29:08 Jonathan Crapuchettes wrote:
> I'm working on an application that requires a large number of strings that
> only need to be loaded once at runtime and need to be accessible to all
> threads throughout the execution of the program. Some of these strings are
> variables like database host and username that need to be read from a file.
> 
> Can anyone help me with an example how they might do this task?
> Thank you,
> JC

immutable string1;
immutable string2;
immtuable string3;

static shared this()
{
    string1 = "the string";
    string2 = "the other string";
    string3 = funcThatGrabsStringFromFile();
}

immutable variables are implicitly shared. The shared module constructor will 
then initialize them before main runs, and all threads will have access to 
them.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list