c++ - C++ 中的共享队列

标签 c++ multithreading boost queue boost-thread

我只是简单地从网络获取数据包,并将它们放入一个线程中,然后在另一个线程中使用这些数据包(出队)。

所以我决定使用 boost 库来创建一个基于 https://www.quantnet.com/cplusplus-multithreading-boost/

template <typename T>
class SynchronisedQueue
{
private:
    std::queue<T> m_queue;  // Use STL queue to store data
    boost::mutex m_mutex;   // The mutex to synchronise on
    boost::condition_variable m_cond;// The condition to wait for

public:

    // Add data to the queue and notify others
    void Enqueue(const T& data)
    {
        // Acquire lock on the queue
        boost::unique_lock<boost::mutex> lock(m_mutex);

        // Add the data to the queue
        m_queue.push(data);

        // Notify others that data is ready
        m_cond.notify_one();

    } // Lock is automatically released here

    // Get data from the queue. Wait for data if not available
    T Dequeue()
    {

        // Acquire lock on the queue
        boost::unique_lock<boost::mutex> lock(m_mutex);

        // When there is no data, wait till someone fills it.
        // Lock is automatically released in the wait and obtained 
        // again after the wait
        while (m_queue.size()==0) m_cond.wait(lock);

        // Retrieve the data from the queue
        T result=m_queue.front(); m_queue.pop();
        return result;

    } // Lock is automatically released here
};

The problem is , while not getting any data, Dequeue() method blocks my consumer thread, and when i want to terminate consumer thread i can not able to end it or stop it sometimes.

结束 Dequeue() 阻塞的建议方法是什么,以便我可以安全地终止消耗数据包的线程? 有什么想法建议吗?

PS:本站https://www.quantnet.com/cplusplus-multithreading-boost/使用“boost::this_thread::interruption_point();”用于停止消费者线程...由于我遗留的代码结构,这对我来说是不可能的...

根据答案,我像这样更新共享队列:

#include <queue>
 #include <boost/thread.hpp>  

template <typename T>
class SynchronisedQueue
{
public:

    SynchronisedQueue()
    {
        RequestToEnd = false;  
        EnqueueData = true;
    }
    void Enqueue(const T& data)
    {
        boost::unique_lock<boost::mutex> lock(m_mutex);

        if(EnqueueData)
        {
            m_queue.push(data);
            m_cond.notify_one();
        }

    } 


    bool TryDequeue(T& result)
    {
        boost::unique_lock<boost::mutex> lock(m_mutex);

        while (m_queue.empty() && (! RequestToEnd)) 
        { 
            m_cond.wait(lock);
        }

        if( RequestToEnd )
        {
             DoEndActions();
             return false;
        }

        result= m_queue.front(); m_queue.pop();

        return true;
    }

    void StopQueue()
    {
        RequestToEnd =  true;
        Enqueue(NULL);        
    }

    int Size()
    {
        boost::unique_lock<boost::mutex> lock(m_mutex);
        return m_queue.size();

    }

private:

    void DoEndActions()
    {
        EnqueueData = false;

        while (!m_queue.empty())  
        {
            m_queue.pop();
        }
    }



    std::queue<T> m_queue;              // Use STL queue to store data
    boost::mutex m_mutex;               // The mutex to synchronise on
    boost::condition_variable m_cond;            // The condition to wait for

    bool RequestToEnd;
    bool EnqueueData;
};

这是我的试驾:

#include <iostream>
#include <string>

#include "SynchronisedQueue.h"

using namespace std;

SynchronisedQueue<int> MyQueue;

void InsertToQueue()
{
    int i= 0;

    while(true)
    {
        MyQueue.Enqueue(++i);
    }

}

void ConsumeFromQueue()
{
    while(true)
    {
        int number;

        cout << "Now try to dequeue" << endl;

        bool success = MyQueue.TryDequeue(number);

        if(success)
        {

            cout << "value is " << number << endl;

        }

        else
        {
            cout << " queue is stopped" << endl;
            break;

        }
    }


    cout << "Que size is : " << MyQueue.Size() <<  endl;
}



int main()
{

    cout << "Test Started" << endl;

    boost::thread startInsertIntoQueue = boost::thread(InsertToQueue);
    boost::thread consumeFromQueue = boost::thread(ConsumeFromQueue);

    boost::this_thread::sleep(boost::posix_time::seconds(5)); //After 5 seconds

    MyQueue.StopQueue();

    int endMain;

    cin >> endMain;


    return 0;
}

目前看来可行...基于新的建议:

我将停止方法更改为:

void StopQueue()
    {
        boost::unique_lock<boost::mutex> lock(m_mutex);
        RequestToEnd =  true;
        m_cond.notify_one();          
    }

最佳答案

让线程结束的 2 个简单解决方案:

  1. 在队列上发送结束消息。
  2. 在条件变量中添加另一个条件命令结束

    while(queue.empty() && (! RequestToEnd)) m_cond.wait(lock);
    if (RequestToEnd) { doEndActions(); }
    else { T result=m_queue.front(); m_queue.pop(); return result; }
    

关于c++ - C++ 中的共享队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139251/

相关文章:

c++ - 类声明混淆 - 右大括号和分号之间的名称

c++ - OpenCV 解决任意系统的返回常量值?

java - android.os.Handler 类是否不需要将某些方法声明为同步?

c++ - 通过套接字发送 boost 序列化时应用程序崩溃

c++ - 在类头中声明类对象 C++

C++静态分配双端队列实现

java - 如何控制ThreadPoolTask​​Executor中正在运行的线程数量?

Java exec方法,如何正确处理流

c++ - boost::multi_index 使用自定义键提取器语法问题?

c++ - 使用 Boost.Log 构建 Boost "error: target { simple_event_log.mc. } has no type"