I need to create a Thread-safe static variable in C# .Net

后端 未结 7 1379
失恋的感觉
失恋的感觉 2020-12-17 18:01

ok, its a little more complicated than the question.

class A
{
   static int needsToBeThreadSafe = 0;

   public static void M1()
   {
     needsToBeThreadSa         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 18:26

    What you might be trying to ask about is the [ThreadStatic] attribute. If you want each thread that uses the class A to have its own separate value of needsToBeThreadSafe then you just need to decorate that field with the [ThreadStatic] attribute.

    For more info refer to the MSDN documentation for ThreadStaticAttribute.

提交回复
热议问题