Thread Safety Mode Enum
Specifies how a Lazy<T> instance synchronizes access among multiple threads.
public enum class LazyThreadSafetyMode
public enum LazyThreadSafetyMode
type LazyThreadSafetyMode =
Public Enum LazyThreadSafetyMode
Locks are used to ensure that only a single thread can initialize a Lazy<T> instance in a thread-safe manner. If the initialization method (or the parameterless constructor, if there is no initialization method) uses locks internally, deadlocks can occur. If you use a Lazy<T> constructor that specifies an initialization method (
The Lazy<T> instance is not thread safe; if the instance is accessed from multiple threads, its behavior is undefined. Use this mode only when high performance is crucial and the Lazy<T> instance is guaranteed never to be initialized from more than one thread. If you use a Lazy<T> constructor that specifies an initialization method (
When multiple threads try to initialize a Lazy<T> instance simultaneously, all threads are allowed to run the initialization method (or the parameterless constructor, if there is no initialization method). The first thread to complete initialization sets the value of the Lazy<T> instance. That value is returned to any other threads that were simultaneously running the initialization method, unless the initialization method throws exceptions on those threads. Any instances of
Use this enumeration to specify the
mode parameter of Lazy<T> constructors. The effects of all constructors on thread synchronization can be described in terms of this enumeration, whether or not they have
A Lazy<T> instance is initialized either by a user-specified initialization method or by the parameterless constructor for
T. The initialization method is specified by the
valueFactory parameter of a Lazy<T> constructor. The method returns an instance of
T, which is the type that is lazily instantiated by the instance of Lazy<T>. If a constructor does not have a
valueFactory parameter, the parameterless constructor for
T is used to initialize the Lazy<T> instance. In either case, initialization occurs the first time you call the Lazy<T>.Value property.
In addition to specifying the thread safety of a Lazy<T> instance, this enumeration affects exception caching. When exceptions are cached for a Lazy<T> instance, you get only one chance to initialize the instance. If an exception is thrown the first time you call the Lazy<T>.Value property, that exception is cached and rethrown on all subsequent calls to the Lazy<T>.Value property. The advantage of caching exceptions is that any two threads always get the same result, even when errors occur.
When you specify the PublicationOnly mode, exceptions are never cached. When you specify None or ExecutionAndPublication, caching depends on whether you specify an initialization method or allow the parameterless constructor for
T to be used. Specifying an initialization method enables exception caching for these two modes. The initialization method can be very simple. For example, it might call the parameterless constructor for
new Lazy<Contents>(() => new Contents(), mode) in C#, or
New Lazy(Of Contents)(Function() New Contents()) in Visual Basic. If you use a constructor that does not specify an initialization method, exceptions that are thrown by the parameterless constructor for
T are not cached. The following table summarizes exception caching behavior.
|Mode||Using initialization method||Using parameterless constructor for
|PublicationOnly||Not cached||Not cached|