Prior to .net 4.0, I implemented a solution using named data slots in System.Threading.Thread. Now, in .net 4.0, there is the idea of ThreadLocal. How does ThreadLocal use compare with named data slots? Does ThreadLocal mean inherited by child threads? Is the idea that ThreadLocal is a simplified version of using named data slots? The following is an example of some material using the named data slots. Could this be simplified with ThreadLocal and will it retain the same properties as named data slots?
public static void SetSliceName(string slice) { System.Threading.Thread.SetData(System.Threading.Thread.GetNamedDataSlot(SliceVariable), slice); } public static string GetSliceName(bool errorIfNotFound) { var slice = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot(SliceVariable)) as string; if (errorIfNotFound && string.IsNullOrEmpty(slice)) {throw new ConfigurationErrorsException("Server slice name not configured.");} return slice; }
source share