在生产者 - 消费者使用情况条件变量 [英] Using condition variable in a producer-consumer situation

查看:157
本文介绍了在生产者 - 消费者使用情况条件变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解条件变量以及如何使用它的生产者 - 消费者的情况。我有一个队列,其中一个线程数推到队列中,而另一个线程从队列中弹出的号码。我想使用的条件变量的信号的耗费线时,有一些数据放置的生产线。问题是,有次(或大部分时间),它仅推到两个项目到队列中,然后挂起。我曾在那里在调试模式下运行时停止生产()函数来表示。谁能帮我指出为什么发生这种情况?

我有以下的全局变量:

 
提高::互斥mutexQ; //互斥体保护队列
提高::互斥mutexCond; //互斥的条件变量
提高:: condition_variable condQ;

下面是我的消费者线程:

 
无效消耗()
{
    而(!bStop层)//全局声明,停止时ESC键是pressed
    {
        提高:: unique_lock锁(mutexCond);
        而(!bDataReady)
        {
            condQ.wait(锁);
        }        // 处理数据
        如果(!messageQ.empty())
        {
            提高::互斥:: scoped_lock的锁(mutexQ);            字符串s = messageQ.front();
            messageQ.pop();
        }
    }
}

下面是我的生产者线程:

 
无效的农产品()
{
    INT I = 0;    而((!bStop层)&&(I&LT MESSAGE))//消息当前设置为10
    {
        字符串流出来;
        出&LT和LT我;
        字符串s = out.str();        提高::互斥:: scoped_lock的锁(mutexQ);
        messageQ.push(多个);        我++;
        {
            提高:: lock_guard锁(mutexCond); //这里挂
            bDataReady =真;
        }
        condQ.notify_one();
    }
}


解决方案

您必须使用相同的互斥,你的条件变量用来守护队列中。

这应该是所有您需要:

 无效消耗()
{
    而(!bStop层)
    {
        提高:: scoped_lock的锁(mutexQ);
        // 处理数据
        而(messageQ.empty())//当 - 守卫agains虚假唤醒
        {
            condQ.wait(锁);        }
        字符串s = messageQ.front();
        messageQ.pop();
    }
}无效的农产品()
{
    INT I = 0;    而((bStop层)及!及(I< MESSAGE))
    {
        字符串流出来;
        出<<一世;
        字符串s = out.str();        提高::互斥:: scoped_lock的锁(mutexQ);
        messageQ.push(多个);
        我++;
        condQ.notify_one();
    }
}

I'm trying to learn about condition variables and how to use it in a producer-consumer situation. I have a queue where one thread pushes numbers into the queue while another thread popping numbers from the queue. I want to use the condition variable to signal the consuming thread when there is some data placed by the producing thread. The problem is there are times (or most times) that it only pushes up to two items into the queue then hangs. I have indicated in the produce() function where it stops when running in debug mode. Can anyone help me point out why this is happening?

I have the following global variables:


boost::mutex mutexQ;               // mutex protecting the queue
boost::mutex mutexCond;            // mutex for the condition variable
boost::condition_variable condQ;

Below is my consumer thread:


void consume()
{
    while( !bStop )   // globally declared, stops when ESC key is pressed
    {
        boost::unique_lock lock( mutexCond );
        while( !bDataReady )
        {
            condQ.wait( lock );
        }

        // Process data
        if( !messageQ.empty() )
        {
            boost::mutex::scoped_lock lock( mutexQ );

            string s = messageQ.front();   
            messageQ.pop();
        }
    }
}

Below is my producer thread:


void produce()
{
    int i = 0;

    while(( !bStop ) && ( i < MESSAGE ))    // MESSAGE currently set to 10
    {
        stringstream out;
        out << i;
        string s = out.str();

        boost::mutex::scoped_lock lock( mutexQ );
        messageQ.push( s );

        i++;
        {
            boost::lock_guard lock( mutexCond );  // HANGS here
            bDataReady = true;
        }
        condQ.notify_one();
    }
}

解决方案

You have to use the same mutex to guard the queue as you use in the condition variable.

This should be all you need:

void consume()
{
    while( !bStop )
    {
        boost::scoped_lock lock( mutexQ);
        // Process data
        while( messageQ.empty() ) // while - to guard agains spurious wakeups
        {
            condQ.wait( lock );

        }
        string s = messageQ.front();            
        messageQ.pop();
    }
}

void produce()
{
    int i = 0;

    while(( !bStop ) && ( i < MESSAGE ))
    {
        stringstream out;
        out << i;
        string s = out.str();

        boost::mutex::scoped_lock lock( mutexQ );
        messageQ.push( s );
        i++;
        condQ.notify_one();
    }
}

这篇关于在生产者 - 消费者使用情况条件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆