Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 23, 2011 Global runtime strings help | ||||
---|---|---|---|---|
| ||||
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 |
September 23, 2011 Re: Global runtime strings help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Crapuchettes | 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
|
September 26, 2011 Re: Global runtime strings help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
|
September 26, 2011 Re: Global runtime strings help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Crapuchettes | 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
|
Copyright © 1999-2021 by the D Language Foundation