Function FiberLocal
Construct for defining new fiber local storage variables.
ref T FiberLocal(T, T initVal = T .init, string id = null, string file = __FILE_FULL_PATH__, string mod = __MODULE__, ulong line = __LINE__)
() @property @trusted;
The file, mod and line template params should be ignored. They are used merely to ensure that each FiberLocal definition is unique.
To use, alias a name to FiberLocal like so:
alias someName = FiberLocal!(uint, 12);
Any reference to someName
will reference a fiber local variable of type uint
with an init value of 12.
Note
It is important to understand that someName
actually aliases a @property
function that returns a reference to said
variable. Under most circumstances this makes no difference. If you wish to take the variable's address, however, you
need to explicitly invoke the function, or you'll get a pointer to the function rather than the variable:
auto ptr1 = &someName;
pragma(msg, typeof(ptr1)); // ref uint function() nothrow @nogc @property @trusted
auto ptr2 = &someName();
pragma(msg, typeof(ptr2)); // uint*
The different FLS variables are distinguished based on their template paramters. Usually this is not a problem, as the source file and line number where the variable is defined is coded. Under some cases, however, this is not unique enough.
static foreach(id; ["fls1", "fls2", "someOtherFls"] ) {
mixin(q{alias %s = FiberLocal!int;} .format(id));
}
The above code creates three variables, called fls1
, fls2
and someOtherFls
, all aliased to the same actual value.
This is because all three are defined on the same source line.
To solve this problem, use the id
template argument. It does not matter what it is, so long as it is unique. The
following code generates three variables, as before, but all three are unique:
static foreach(id; ["fls1", "fls2", "someOtherFls"] ) {
mixin(q{alias %s = FiberLocal!(int, int.init, id);} .format(id));
}
Parameters
Name | Description |
---|---|
T | The type of the FLS variable |
initVal | The variable initial value |
id | Optional identifier for defining multiple FLSes from the same line of code |