title: Thread Synchronisation 101
body: <p>Previously I've written some very simple multithreaded code, and I've always been aware that at any time there could be a context switch right in the middle of what I'm doing, so I've always guarded access the shared variables through a CCriticalSection class that enters the critical section on construction and leaves it on destruction. I know this is fairly aggressive and I enter and leave critical sections quite frequently and sometimes egregiously (e.g. at the start of a function when I could put the CCriticalSection inside a tighter code block) but my code doesn't crash and it runs fast enough.</p>
  
  <p>At work my multithreaded code needs to be a tighter, only locking/synchronising at the lowest level needed.</p>
  
  <p>At work I was trying to debug some multithreaded code, and I came across this:</p>
  
  <pre><code>EnterCriticalSection(&amp;m_Crit4);
  m_bSomeVariable = true;
  LeaveCriticalSection(&amp;m_Crit4);
  </code></pre>
  
  <p>Now, <code>m_bSomeVariable</code> is a Win32 BOOL (not volatile), which as far as I know is defined to be an int, and on x86 reading and writing these values is a single instruction, and since context switches occur on an instruction boundary then there's no need for synchronising this operation with a critical section.</p>
  
  <p>I did some more research online to see whether this operation did not need synchronisation, and I came up with two scenarios it did:</p>
  
  <ol>
  <li>The CPU implements out of order execution or the second thread is running on a different core and the updated value is not written into RAM for the other core to see; and</li>
  <li>The int is not 4-byte aligned.</li>
  </ol>
  
  <p>I believe number 1 can be solved using the "volatile" keyword. In VS2005 and later the C++ compiler surrounds access to this variable using memory barriers, ensuring that the variable is always completely written/read to the main system memory before using it.</p>
  
  <p>Number 2 I cannot verify, I don't know why the byte alignment would make a difference. I don't know the x86 instruction set, but does <code>mov</code> need to be given a 4-byte aligned address? If not do you need to use a combination of instructions? That would introduce the problem.</p>
  
  <p>So...</p>
  
  <p><strong>QUESTION 1:</strong> Does using the "volatile" keyword (implicity using memory barriers and hinting to the compiler not to optimise this code) absolve a programmer from the need to synchronise a 4-byte/8-byte on x86/x64 variable between read/write operations?</p>
  
  <p><strong>QUESTION 2:</strong> Is there the explicit requirement that the variable be 4-byte/8-byte aligned?</p>
  
  <p>I did some more digging into our code and the variables defined in the class:</p>
  
  <pre><code>class CExample
  {
  
  private:
  
      CRITICAL_SECTION m_Crit1; // Protects variable a
      CRITICAL_SECTION m_Crit2; // Protects variable b
      CRITICAL_SECTION m_Crit3; // Protects variable c
      CRITICAL_SECTION m_Crit4; // Protects variable d
  
      // ...
  
  };
  </code></pre>
  
  <p>Now, to me this seems excessive. I thought critical sections synchronised threads between a process, so if you've got one you can enter it and no other thread in that process can execute. There is no need for a critical section for each variable you want to protect, if you're in a critical section then nothing else can interrupt you.</p>
  
  <p>I think the only thing that can change the variables from outside a critical section is if the process shares a memory page with another process (can you do that?) and the other process starts to change the values. Mutexes would also help here, named mutexes are shared across processes, or only processes of the same name?</p>
  
  <p><strong>QUESTION 3:</strong> Is my analysis of critical sections correct, and should this code be rewritten to use mutexes? I have had a look at other synchronisation objects (semaphores and spinlocks), are they better suited here?</p>
  
  <p><strong>QUESTION 4:</strong> Where are critical sections/mutexes/semaphores/spinlocks best suited? That is, which synchronisation problem should they be applied to. Is there a vast performance penalty for choosing one over the other?</p>
  
  <p>And while we're on it, I read that spinlocks should not be used in a single-core multithreaded environment, only a multi-core multithreaded environment. So, <strong>QUESTION 5:</strong> Is this wrong, or if not, why is it right?</p>
  
  <p>Thanks in advance for any responses :)</p>