mercredi 20 décembre 2017

C++11 - Difficulty with unique_lock in simple Producer & Consumer

I have written a very simple Producer/Consumer based out of C++11 notes and unable to figure out why the unique_lock() does not release the lock when goes out of scope.

Here is the code:-

struct Message{
Message(int x):data(x){cout<<"+";}
int data;
};


queue<Message*> g_queue;
condition_variable cv;
mutex m;

void consumer()
{
    do {
        unique_lock<mutex> lck {m};
        cv.wait(lck, [&](){return !g_queue.empty();});
        cout<<"Notified...";
        auto& obj = g_queue.front();
        std::cout<<obj->data;
        g_queue.pop();
        cout<<".";
        lck.unlock(); -----(1)
    } while(1);

}

void producer() 
{
    while(true){
            unique_lock<mutex> lck {m};
            Message msg{5};
            cout<<"Queue size:"<<g_queue.size()<<'\n';
            g_queue.push(&msg);
            cv.notify_one();
            lck.unlock(); -------(2)
            cout<<"-"<<'\n';
            this_thread::sleep_for(std::chrono::milliseconds{2000});
    }
}

And use it as:-

    thread Q(&consumer);
    thread P(&producer);
    P.join();
    Q.join();

The output is:-

+Queue size:0
-Notified...
5.+Queue size:0
-Notified...5
.+Queue size:0
-Notified...5

So technically, yes, Producer needs to tell Consumer that I am ready and Consumer needs to let Producer know send more data. I am not clear on what to use, does condition variable does this or does unique_lock does this.

Precisely, why I need (1) and (2) when the scope can release the locks

Aucun commentaire:

Enregistrer un commentaire