www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Global runtime strings help

reply Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
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
Sep 23 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 23 2011
parent reply Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
Thank you for the thought, but the problem here is that the file containing the 
strings is only known at runtime from a command line argument. I also have some 
global strings that need to be set from the database.

Thank you again,
JC

Jonathan M Davis wrote:
 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
Sep 26 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 26 Sep 2011 15:57:21 -0400, Jonathan Crapuchettes  
<jcrapuchettes gmail.com> wrote:

 Thank you for the thought, but the problem here is that the file  
 containing the strings is only known at runtime from a command line  
 argument. I also have some global strings that need to be set from the  
 database.

 Thank you again,
 JC
Hm... interesting situation. The issue is, you want them to be immutable at some arbitrary point in time, NOT before main is run. With D the way it is, I think you are better off encapsulating that as private mutable storage backing public accessors: module mystringdata; private shared /* or __gshared */ string _mystringvalue = null; void initializeStrings(dbconnection db, someFileSource f) // call this before using any of the strings { // read the string from the db/file _mystringvalue = db.read("mystringdata"); ... } property string mystringvalue() { assert(_mystringvalue !is null); // ensure it's valid before being used. return _mystringvalue; } // repeat for other values. Otherwise, you could potentially circumvent the type system, but that results in undefined behavior. I don't know how well that would work, but it might solve the problem for the current compiler implementation. -Steve
Sep 26 2011