I have a producer consumer that simulates the interaction of threads controlling a hardware device. I have 1 thread controlling the device, 1 thread reading, and 1 thread outputting.  The read is synchronous on the thread while the controlling is asynchronous. My producer is the thread controlling the device and my consumer is a thread that processes the data. Often the consumer takes about the same amount of time as the producer (ideal), but sometimes it takes more time.The hope is  that the producer should not produce more than the consumer can process. This is partly due to hardware overflow, the consumer will free a buffer on the device, and partly due to visual latency.I think the producer should be responsible for regulating the rate of production. I implemented this regulation using a sleep timeout.I am wondering if this is the best way to do it.My heuristic for success is the time output of the redraw command, which should be roughly ConsumerTime*8 (maybe 400).#include <cstdlib>#include <thread>#include <condition_variable>#include <iostream>#include <atomic>#include <chrono>//UTILITY//using namespace std;#if !defined(WIN32)#include <unistd.h>void_sleep (int ms){    usleep (ms * 1000); //convert to microseconds}#endiflong longtimestamp (){    auto now = std::chrono::system_clock::now ();    auto duration = now.time_since_epoch ();    auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count ();    return millis;}//REDRAW//bool killRedraw;bool drawIt;long long last;voidredraw (){    cout << Starting drawing thread << endl;    last = timestamp ();    while (!killRedraw)    {        if (drawIt)        {            long long now = timestamp ();            cout << I drew it: << now - last << endl;            last = now;            drawIt = false;        }        _sleep (15);    }}//PRODUCER//atomic<int> balance;bool killProducer;voidproduce (){    cout << Made one: << ++balance << endl;}voidproducer (){    balance = 0;    cout << Starting producer thread << endl;    while (!killProducer)    {        _sleep (40);        produce ();        while (balance > 1)        {            _sleep (10);        }    }}//CONSUMER//bool killConsumer;voidconsume (){    cout << Ate one: << --balance << endl;}voidconsumer (){    cout << Starting consumer thread << endl;    while (!killConsumer)    {        int basetime = 35;        int noise = rand () % 20;        _sleep (basetime + noise);        consume ();        static int cycle = -1;        if ((cycle = ++cycle % 8) == 7)        {            drawIt = true;        }    }}//Main//intmain (int argc, char** argv){    //    // Important metric is the draw it time, which should be about 8*50    //    killProducer = false;    std::thread tP (producer);    killConsumer = false;    std::thread tC (consumer);    killRedraw = false;    drawIt = false;    std::thread tD (redraw);    _sleep (10000);    killProducer = true;    killConsumer = true;    killRedraw = true;    tD.join ();    tP.join ();    tC.join ();    //    return 0;}
Producer consumer that simulates the interaction of threads controlling a hardware device
c++;performance;design patterns;multithreading;c++11