static void ThreadProc(object state) { LargeObject large = lazyLargeObject.Value; // IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the // object after creation. You must lock the object before accessing it, // unless the type is thread safe. (LargeObject is not thread safe.) lock (large) { large.Data[0] = Thread.CurrentThread.ManagedThreadId; Console.WriteLine("Initialized by thread {0}; last used by thread {1}.", large.InitializedBy, large.Data[0]); } }
static LargeObject InitLargeObject() { LargeObject large = new LargeObject(Thread.CurrentThread.ManagedThreadId); return(large); }