Search code examples
c#multithreadingthread-local-storage

Why use data slot over ThreadLocal?


Comparing unnamed data slots and ThreadLocal...

MSDN only says

In the .NET Framework 4, you can use the System.Threading.ThreadLocal class to create thread-local objects that are initialized lazily when the object is first consumed. For more information, see Lazy Initialization.

If I'm fine with lazy init, ThreadLocal seems like the best choice... it's simpler to use and doesn't require casting. Is there any other things to consider when choosing one over the other?


Solution

  • As I said in comments, ThreadLocal<T> is implemented using the Thread Local Dataslots API. You can confirm that by looking at the ThreadLocal source code. Unless you have some compelling reason to work at the Windows API level, you're better off using ThreadLocal<T>.

    One very good reason to choose ThreadLocal<T> is that it makes sure that the value is initialized for every thread. ThreadStatic does not do that, and your interface to thread local data slots API would have to code specially for it, too. See https://stackoverflow.com/a/18337158/56778 for a bit more info.

    Use ThreadLocal<T> with lazy initialization. You'll save yourself a lot of pain.