-3

Suppose i have a class with a global variable a method and couple of threads.

    class Class
    {
      public var Var1;

      piblic mainFoo()
      {
          thread1.start(thread_foo1);
          thread2.start(thread_foo2);
      }
      public void Foo(var1, var2, var3)
      {
       ...
      }

      private thread_foo1()
      {

        if(Var1) {}
        Foo(_var1,_var2,_var3);

      }

      private thread_foo2()
      {

        if(Var1) {}
        Foo(_var1,_var2,_var3);

      }

    }

Var1 is not changed anywhere but the main thread. Foo does not change any data in any of threads, it's just some small common code that i do not want to repeat. Will this structure cause any problems or conflicts in the threads?

KonB
  • 220
  • 1
  • 10
  • Does you main thread update the variable aside from initially? – johnny 5 Mar 21 '17 at 20:01
  • 1
    No, as long as two threads can run in `Foo` concurrently, it is fine... But that condition is not always the case. – Willem Van Onsem Mar 21 '17 at 20:01
  • @WillemVanOnsem What about a torn read? – johnny 5 Mar 21 '17 at 20:02
  • @johnny5: a torn read only occurs if some thread **writes** to the object you read. In the question it is said that none of the threads *write*. – Willem Van Onsem Mar 21 '17 at 20:03
  • 1
    OP: `Var1 is not changed anywhere but the main thread` which implies the main thread can change the variable, Var1 is declared in properly in the example `var Var1` if its a bool everything should be (assumed to be) okay – johnny 5 Mar 21 '17 at 20:03
  • @john5: well I assumed that the main thread is suspended until the two threads have finished. Please do not use backquotes when citing. It is hard for screenreaders to process that (letter-by-letter). Use *italics*. – Willem Van Onsem Mar 21 '17 at 20:04
  • @WillemVanOnsem under that assumption everything should be fine yeah, the OP needs to clarify his use case better – johnny 5 Mar 21 '17 at 20:06
  • Maybe I should have clarified. The thread1 and thread2 run independently and never terminate, unless main thread terminates. Var1 in the main thread will occasionally change it's value, but it's a simple bool "true/false" deal, based on a timer running under main thread. – KonB Mar 21 '17 at 20:09
  • @WillemVanOnsem There's no point in creating new threads if you're just going to suspend them while others are working; at that point you're better off just not creating the additional thread(s) in the first place. – Servy Mar 21 '17 at 20:49
  • @KonB Nobody can tell you if code you don't show anyone works or not. – Servy Mar 21 '17 at 20:50

1 Answers1

1

It depends on type of the global variable.

1) Global variable is value-type

2) Global variable is reference-type

  • Reference assignment guaranteed to be atomic, so assignment can be safely done from any thread
  • If type is immutable or stateless, you can assume reading from other threads is safe (for example string)
  • If type is guaranteed to be thread-safe, you can assume reading from other threads is safe (for example ConcurentDictionary)
  • Otherwise reading (or more precisely accessing internal state through variable's properties) is not safe as any logic over variable's properties can lead to inconsistent internal state during reading from other threads (for example Dictionary)
Community
  • 1
  • 1
Lanorkin
  • 7,310
  • 2
  • 42
  • 60