System.Threading.LazyThreadSafetyMode Enumeration

Specifies how a Lazy`1 instance synchronizes access among multiple threads.

Syntax

public enum LazyThreadSafetyMode

Remarks

Use this enumeration to specify the mode parameter of Lazy`1 constructors. The effects of all constructors on thread synchronization can be described in terms of this enumeration, whether or not they have mode parameters.

A Lazy`1 instance is initialized either by a user-specified initialization method or by the default constructor for T. The initialization method is specified by the valueFactory parameter of a Lazy`1 constructor. The method returns an instance of T, which is the type that is lazily instantiated by the instance of Lazy`1. If a constructor does not have a valueFactory parameter, the default constructor for T is used to initialize the Lazy`1 instance. In either case, initialization occurs the first time you call the Lazy`1.Value property.

In addition to specifying the thread safety of a Lazy`1 instance, this enumeration affects exception caching. When exceptions are cached for a Lazy`1 instance, you get only one chance to initialize the instance. If an exception is thrown the first time you call the Lazy`1.Value property, that exception is cached and rethrown on all subsequent calls to the Lazy`1.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 LazyThreadSafetyMode.PublicationOnly mode, exceptions are never cached. When you specify LazyThreadSafetyMode.None or LazyThreadSafetyMode.ExecutionAndPublication, caching depends on whether you specify an initialization method or allow the default 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 default constructor for T: 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 default constructor for T are not cached. The following table summarizes exception caching behavior.

LazyThreadSafetyMode.None

Cached

Not cached

LazyThreadSafetyMode.PublicationOnly

Not cached

Not cached

LazyThreadSafetyMode.ExecutionAndPublication

Cached

Not cached

Members

Member NameDescription
ExecutionAndPublication

Locks are used to ensure that only a single thread can initialize a Lazy`1 instance in a thread-safe manner. If the initialization method (or the default constructor, if there is no initialization method) uses locks internally, deadlocks can occur. If you use a Lazy`1 constructor that specifies an initialization method (valueFactory parameter), and if that initialization method throws an exception (or fails to handle an exception) the first time you call the Lazy`1.Value property, then the exception is cached and thrown again on subsequent calls to the Lazy`1.Value property. If you use a Lazy`1 constructor that does not specify an initialization method, exceptions that are thrown by the default constructor for T are not cached. In that case, a subsequent call to the Lazy`1.Value property might successfully initialize the Lazy`1 instance. If the initialization method recursively accesses the Lazy`1.Value property of the Lazy`1 instance, an InvalidOperationException is thrown.

None

The Lazy`1 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`1 instance is guaranteed never to be initialized from more than one thread. If you use a Lazy`1 constructor that specifies an initialization method (valueFactory parameter), and if that initialization method throws an exception (or fails to handle an exception) the first time you call the Lazy`1.Value property, then the exception is cached and thrown again on subsequent calls to the Lazy`1.Value property. If you use a Lazy`1 constructor that does not specify an initialization method, exceptions that are thrown by the default constructor for T are not cached. In that case, a subsequent call to the Lazy`1.Value property might successfully initialize the Lazy`1 instance. If the initialization method recursively accesses the Lazy`1.Value property of the Lazy`1 instance, an InvalidOperationException is thrown.

PublicationOnly

When multiple threads try to initialize a Lazy`1 instance simultaneously, all threads are allowed to run the initialization method (or the default constructor, if there is no initialization method). The first thread to complete initialization sets the value of the Lazy`1 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 T that were created by the competing threads are discarded. If the initialization method throws an exception on any thread, the exception is propagated out of the Lazy`1.Value property on that thread. The exception is not cached. The value of the Lazy`1.IsValueCreated property remains false, and subsequent calls to the Lazy`1.Value property, either by the thread where the exception was thrown or by other threads, cause the initialization method to run again. If the initialization method recursively accesses the Lazy`1.Value property of the Lazy`1 instance, no exception is thrown.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 4.0.0.0