The OpenD Programming Language

initOnce

Initializes var with the lazy init value in a thread-safe manner.

The implementation guarantees that all threads simultaneously calling initOnce with the same var argument block until var is fully initialized. All side-effects of init are globally visible afterwards.

Parameters

var

The variable to initialize

init typeof(var)

The lazy initializer value

Return Value

Type: auto ref

A reference to the initialized variable

Examples

A typical use-case is to perform lazy but thread-safe initialization.

static class MySingleton
{
    static MySingleton instance()
    {
        __gshared MySingleton inst;
        return initOnce!inst(new MySingleton);
    }
}

assert(MySingleton.instance !is null);

Meta