<legend id='uiovF'><style id='uiovF'><dir id='uiovF'><q id='uiovF'></q></dir></style></legend>

  1. <i id='uiovF'><tr id='uiovF'><dt id='uiovF'><q id='uiovF'><span id='uiovF'><b id='uiovF'><form id='uiovF'><ins id='uiovF'></ins><ul id='uiovF'></ul><sub id='uiovF'></sub></form><legend id='uiovF'></legend><bdo id='uiovF'><pre id='uiovF'><center id='uiovF'></center></pre></bdo></b><th id='uiovF'></th></span></q></dt></tr></i><div id='uiovF'><tfoot id='uiovF'></tfoot><dl id='uiovF'><fieldset id='uiovF'></fieldset></dl></div>

    <small id='uiovF'></small><noframes id='uiovF'>

  2. <tfoot id='uiovF'></tfoot>

    • <bdo id='uiovF'></bdo><ul id='uiovF'></ul>

    1. 在生产者-消费者情况下使用条件变量

      Using condition variable in a producer-consumer situation(在生产者-消费者情况下使用条件变量)
      • <bdo id='sylhJ'></bdo><ul id='sylhJ'></ul>

          <legend id='sylhJ'><style id='sylhJ'><dir id='sylhJ'><q id='sylhJ'></q></dir></style></legend>
          <i id='sylhJ'><tr id='sylhJ'><dt id='sylhJ'><q id='sylhJ'><span id='sylhJ'><b id='sylhJ'><form id='sylhJ'><ins id='sylhJ'></ins><ul id='sylhJ'></ul><sub id='sylhJ'></sub></form><legend id='sylhJ'></legend><bdo id='sylhJ'><pre id='sylhJ'><center id='sylhJ'></center></pre></bdo></b><th id='sylhJ'></th></span></q></dt></tr></i><div id='sylhJ'><tfoot id='sylhJ'></tfoot><dl id='sylhJ'><fieldset id='sylhJ'></fieldset></dl></div>

            <tfoot id='sylhJ'></tfoot>

            <small id='sylhJ'></small><noframes id='sylhJ'>

                  <tbody id='sylhJ'></tbody>
                本文介绍了在生产者-消费者情况下使用条件变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试了解条件变量以及如何在生产者-消费者的情况下使用它.我有一个队列,其中一个线程将数字推入队列,而另一个线程从队列中弹出数字.当生产线程放置一些数据时,我想使用条件变量向消费线程发出信号.问题是有时(或大多数时候)它只将最多两个项目推入队列然后挂起.我已经在 generate() 函数中指出它在调试模式下运行时停止的位置.谁能帮我指出为什么会这样?

                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?

                我有以下全局变量:

                
                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();
                    }
                }
                

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                • <bdo id='ETxmQ'></bdo><ul id='ETxmQ'></ul>
                    <tbody id='ETxmQ'></tbody>

                    <tfoot id='ETxmQ'></tfoot>
                    • <small id='ETxmQ'></small><noframes id='ETxmQ'>

                      <legend id='ETxmQ'><style id='ETxmQ'><dir id='ETxmQ'><q id='ETxmQ'></q></dir></style></legend>

                          <i id='ETxmQ'><tr id='ETxmQ'><dt id='ETxmQ'><q id='ETxmQ'><span id='ETxmQ'><b id='ETxmQ'><form id='ETxmQ'><ins id='ETxmQ'></ins><ul id='ETxmQ'></ul><sub id='ETxmQ'></sub></form><legend id='ETxmQ'></legend><bdo id='ETxmQ'><pre id='ETxmQ'><center id='ETxmQ'></center></pre></bdo></b><th id='ETxmQ'></th></span></q></dt></tr></i><div id='ETxmQ'><tfoot id='ETxmQ'></tfoot><dl id='ETxmQ'><fieldset id='ETxmQ'></fieldset></dl></div>