title: Fast lock for variables that are read a lot and may be changed from another thread occasionally
body: <p>I'm looking for a lock, that allows a thread-safe transition between the GUI and the back-end.</p>
  
  <p>Just for a double, but I'm sure it will end up being used for other things. </p>
  
  <p>Now this is the part I'm unsure of, on modern CPUs, can reading and writing at the same time cause a race condition? Or is it only when two threads try to write at the same time.</p>
  
  <p>I've always encapsulated any variables that I cross threads with, in a template object, which allows me the same use but requires locking, here is the basics:</p>
  
  <pre><code>//=====================================================================================================
  // Class to store a variable in a thread safe manner.
  //=====================================================================================================
  template &lt;class T&gt;
  class ThreadSafeVariable
  {
  public:
  
    ThreadSafeVariable(const T &amp; variable):
      _stored_variable(variable)
    {
    }
  
    ThreadSafeVariable():
      _stored_variable()
    {
    }
  
    //=====================================================================================================
    /// Returns the stored variable
    //=====================================================================================================
    T Read() const
    {
      boost::unique_lock&lt;boost::mutex&gt; lock(_mutex);
      return _stored_variable;
    }
  
  
    //=====================================================================================================
    /// Sets the variable
    //=====================================================================================================
    void operator = (const T &amp;value_to_set)
    {
      boost::unique_lock&lt;boost::mutex&gt; lock(_mutex);
      _stored_variable = value_to_set;
    }
  
  
    //=====================================================================================================
    /// Returns the stored variable
    //=====================================================================================================
    operator T() const
    {
      boost::unique_lock&lt;boost::mutex&gt; lock(_mutex);
      return (T) _stored_variable;
    }
  
  
    void SetFromString (const std::string &amp; value_to_set);
    T operator ++ (int);
    T operator -- (int);
    std::string ToString() const;
  
    protected:
    T _stored_variable;
    mutable boost::mutex _mutex;
  };
  </code></pre>
  
  <p>Would it be possible, to make a class like this faster, provided that only one thread was given the option of writing (that part would needed to be encoded by calling different functions).</p>
  
  <p>Basically I've got a static function, that I want to keep static that varies based on a parameter I would like to change on the GUI but it's in a performance critical part of the software. </p>
  
  <p>I'm aware of spin locks, atomics. But have never really used them. I guess a spin lock would be a waste of CPU and I'm not sure of the performance gains from atomics.</p>