Multithreading decouple update-logic and render-logic I want to improve my multithreading competence and tried to build a compact doublebuffered way to separate two threads where one is only displaying the state and the other one updates the state. So I am here to hear whether I done it "right" and what to improve.

#include &lt;thread&gt;
#include &lt;mutex&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;

template &lt;class T&gt; class StateBuffer
{
public:
    typedef T value_t;

    void Swap()
    {
        {
            std::lock_guard&lt;std::mutex&gt; _(swapMutex);
            index = 1 - index;
        }
        Write() = Read();
    }
    value_t&amp; Write()
    {
        std::lock_guard&lt;std::mutex&gt; _(swapMutex);
        return states[index];
    }
    const value_t Read()
    {
        std::lock_guard&lt;std::mutex&gt; _(swapMutex);
        return states[1-index];
    }
    const value_t&amp;&amp; ReadRef()
    {
        std::lock_guard&lt;std::mutex&gt; _(swapMutex);
        return std::ref(states[1-index]);
    }
private:
    mutable std::mutex swapMutex;
    T states[2];
    uint8_t index = 0; // index = write, 1-index = read
};

struct state_t
{
    int count = 0;
    bool stop = false;
};
typedef StateBuffer&lt;state_t&gt; TestStateBuffer;
TestStateBuffer state;

auto constexpr NO_TIME = std::chrono::seconds(0);

void r()
{
    std::ofstream log("log.txt");
    bool run = true;
    while (run)
    {
        state_t display = state.Read();

        log &lt;&lt; display.count &lt;&lt; "\n";
        run = !display.stop;

        std::this_thread::sleep_for(NO_TIME);
    }
    log.flush();
}

void u()
{
    bool run = true;

    while (run)
    {
        state_t&amp; write = state.Write();

        run = !write.stop;
        write.stop = ++write.count &gt; 1000000;

        state.Swap();
        std::this_thread::sleep_for(NO_TIME);
    }
}

int main(int, char**)
{
    std::thread render(r);
    std::thread update(u);

    update.join();
    render.join();

    std::cout &lt;&lt; "ende" &lt;&lt; std::endl;
    std::cin.get();

    return 0;
}


Yes, this is intended to find use in gamedev.

Questions that have gone through my mind:  


I use sleep(0) to force a thread swap. Do I need this?  
Is there a way how to solve this with a conditional_variable as I read that locking a mutex is expensive in comparison to check with a conditional_variable (although one still need a mutex)?  
As I am using references is this really safe?  
Have I left anything out?


Environment: Windows 7 x64 g++ on MinGW / Mint 17 x64 clang++ (both C++11 enabled)
No. Using sleep is the wrong way in any most cases that you can think of. Use the correct functions for this. The C++ standard does not contain a standardized way of doing so. Please use SwitchToThread for Windows and pthread_yield for Unix. To gain more information, you may ask a search engine and for context switching, this is a more common identifier than swapping.
Yes, you are right. Mutual exclusion is indeed slower than a condition variable, whereas it is not always replaceable. Since I don't know how your compiler implements mutexes, you may want to search for differences between mutex and critical sections on Windows. I don't know if there are two different ways also on Unix and other systems. But to answer your question: Yes, one can solve this with condition variables. The difference is very-well stated here.
References are as safe as pointers, since all references are pointers internally after the compiler has done his work. Just be sure not to pass references, that you are expecting to own a resource exclusively, to functions which don't own the resource at all. This leads to very unpredictable code, as wrong coding in multi threaded environments always does.
Seems to be okay. But don't count on my word in the case of the move-operator. I simply don't know if this is going to be valid.