jeudi 23 novembre 2017

Right way to concurrently process std::stack

This might be a basic question in terms of multithreaded programming but I really want to achieve the following without any concurrent data structure. Consider the code:

class A
{
    std::stack<int> s;
public:
    A()
    {
        s.push(7); s.push(6); s.push(5); s.push(4); s.push(3); s.push(2); s.push(1);
    }
    void process(int tid)
    {
        while (!s.empty())
        {
            std::unique_lock<std::mutex> lck(m);
            std::cout << tid << " --> " << s.top() << '\n';
            cv.wait(lck);
            s.pop();
            cv.notify_all();
            lck.unlock();
        }
    }
    std::mutex m;
    std::condition_variable cv;
};

int main()
{   
    A a;
    std::thread t1(&A::process, &a, 1);
    std::thread t2(&A::process, &a, 2);    
    t1.join();
    t2.join();
}

I want for each thread to print the top of the stack and pop it out so that the output is looking like this:

1 --> 1
2 --> 2
1 --> 3
2 --> 4
...

So only 1 thread should enter the while body and execute it only once.

But instead it always outputs:

1 --> 1
2 --> 1 

then it waits infinitely

How can I do this ?

What's wrong with the current solution ?

Aucun commentaire:

Enregistrer un commentaire