Producer consumer in C++11 I'm trying to practice some C++ and I wrote a simple queue to solve the Producer-Consumer Problem.

class Queue
{

public:
Queue() : mutex(), cv(), queue(), process(true), consumer(&amp;Queue::dequeue, this)
{
}

~Queue()
{
    process = false;
    consumer.join();
}

void enqueue(int i)
{
    std::lock_guard&lt;std::mutex&gt; lock(mutex);
    queue.push(i);
    cv.notify_all();
}

void dequeue()
{
    while(process)
    {
        std::unique_lock&lt;std::mutex&gt; lock(mutex);
        while(queue.empty())
        {
            cv.wait(lock);
        }

        if(!process)
        {
            break;
        }

        int i = queue.front();
        queue.pop();
        std::cout &lt;&lt; "Got " &lt;&lt; i &lt;&lt; '\n';
    }
}

private:
std::mutex mutex;
std::condition_variable cv;
std::queue&lt;int&gt; queue;
bool process;
std::thread consumer;

};


Is my implementation good? Is there anything I could've done better?
Dead Lock Bug

If the queue is empty and the consumer thread is waiting in the condition variable. If you Queue goes out of scope the main thread will enter the detructor and perform join(). At this point you are locked. As both threads are waiting for the other.

Use of condition can be simplified.

    while(queue.empty())
    {
        cv.wait(lock);
    }


Can be replaced with:

    cv.wait(lock, [&amp;queue](){return !queue.empty();});


When poping things from a queue use move.

// this performs a copy.
int i = queue.front();

// this should move the value from the que.
int i = std::move(queue.front());


Note: It makes little difference for an integer but if there is an object in the queue then it might.

Formatting

Your code is horribly formatted. Please do a better job in the future. Formatting is a critical part of coding. This is because your code will be read fro decades aftr you have written it by other humans.