Is mutex mandatory to access extern variable from a different thread?
<p>I am developing an application in Qt/C++. At some point, there are two threads : one is the UI thread and the other one is the background thread. I have to do some operation from the background thread based on the value of an extern variable which is type of <code>bool</code>. I am setting this value by clicking a button on UI.</p>

<p><strong>header.cpp</strong></p>

<pre><code>extern bool globalVar;
</code></pre>

<p><strong>mainWindow.cpp</strong></p>

<pre><code>//main ui thread on button click
setVale(bool val){
 globalVar = val;
}
</code></pre>

<p><strong>backgroundThread.cpp</strong></p>

<pre><code> while(1){
  if(globalVar)
    //do some operation
  else
    //do some other operation
  }
</code></pre>

<p>Here, writing to <code>globalVar</code> happens only when the user clicks the button whereas reading happens continuously.</p>

<p>So my question is :</p>

<ol>
<li>In a situation like the one above, is mutex mandatory?</li>
<li>If read and write happens at the same time, does this cause the application to crash?</li>
<li>If read and write happens at same time, is <code>globalVar</code> going to have some value other than <code>true</code> or <code>false</code>?</li>
<li>Finally, does the OS provide any kind of locking mechanism to prevent the read/write operation to access a memory location at the same time by a different thread?</li>
</ol>