using System; using System.Collections.Generic; namespace System.Threading { public class ThreadLocal { Func _factory; Dictionary _cache = new Dictionary(); public ThreadLocal() : this(() => default(T)) { } public ThreadLocal(Func valueFactory) { _factory = valueFactory; } public T Value { get { lock(_cache) { int key = Thread.CurrentThread.ManagedThreadId; if (_cache.ContainsKey(key)) { return _cache[key]; } return (_cache[key] = _factory()); } } set { lock(_cache) { int key = Thread.CurrentThread.ManagedThreadId; _cache[key] = value; } } } } } #if WINDOWS_PHONE || DOTNETISOLDANDSAD namespace System.Concurrency { public class Lazy { public Lazy(Func ValueFetcher) { _Value = ValueFetcher(); } T _Value; T Value { get { return _Value; } } } } #endif // vim: tw=120 ts=4 sw=4 et :