Thread Local Storage For C# Class Library

后端 未结 4 1702
不思量自难忘°
不思量自难忘° 2021-01-04 08:06

I have a very old but very large library which I am considering converting to a C# class library. The existing library uses a lot of global variables stored in the TLS. C# h

4条回答
  •  花落未央
    2021-01-04 08:30

    You can achieve the same thread local storage using the [ThreadStatic] attribute or in .Net 4 by using the ThreadLocal class.

    [ThreadStatic]    
    private static string MyThreadGlobal;
    
    private ThreadLocal MyThreadGlobal = new ThreadLocal();
    

    There's also the CallContext class but the other approaches are probably preferred.

提交回复
热议问题