title: Are checks for null thread-safe?
body: <p>I have some code where exceptions are thrown on a new Thread which I need to acknowledge and deal with on the Main Thread. To achieve this I am sharing state between threads by using a field which holds the thrown exception.</p>
  
  <p>My question is <strong>do I need to use locks when checking for null</strong> as I am doing in the following code sample?</p>
  
  <pre><code>public class MyClass
  {
      readonly object _exceptionLock = new object();
      Exception _exception;
  
      public MyClass()
      {
          Task.Run(() =&gt;
          {
              while (CheckIsExceptionNull())
              {
                  // This conditional will return true if 'something has gone wrong'.
                  if(CheckIfMyCodeHasGoneWrong())
                  {
                      lock(_exceptionLock)
                      {
                          _exception = new GoneWrongException();
                      }
                  }
              }
          });
      }
  
      bool CheckIsExceptionNull() // Is this method actually necessary?
      {
          lock (_exceptionLock)
          {
              return _exception == null;
          }
      }
  
      // This method gets fired periodically on the Main Thread.
      void RethrowExceptionsOnMainThread()
      {
          if (!CheckIsExceptionNull())
          {
              lock (_exceptionLock)
              {
                  throw _exception; // Does this throw need to be in a lock?
              }
          }
      }
  }
  </code></pre>
  
  <p>Additionally, <strong>do I need to use a lock when throwing the exception on the Main Thread?</strong></p>