c++ 线程消息队列 超时

创建日期: 2024-05-29 18:24 | 作者: 风波 | 浏览次数: 13 | 分类: C++

来源:https://www.geeksforgeeks.org/implement-thread-safe-queue-in-c/

一:没有超时的

// C++ implementation of the above approach 
#include <condition_variable> 
#include <iostream> 
#include <mutex> 
#include <queue> 

// Thread-safe queue 
template <typename T> 
class TSQueue { 
private: 
    // Underlying queue 
    std::queue<T> m_queue; 

    // mutex for thread synchronization 
    std::mutex m_mutex; 

    // Condition variable for signaling 
    std::condition_variable m_cond; 

public: 
    // Pushes an element to the queue 
    void push(T item) 
    { 

        // Acquire lock 
        std::unique_lock<std::mutex> lock(m_mutex); 

        // Add item 
        m_queue.push(item); 

        // Notify one thread that 
        // is waiting 
        m_cond.notify_one(); 
    } 

    // Pops an element off the queue 
    T pop() 
    { 

        // acquire lock 
        std::unique_lock<std::mutex> lock(m_mutex); 

        // wait until queue is not empty 
        m_cond.wait(lock, 
                    [this]() { return !m_queue.empty(); }); 

        // retrieve item 
        T item = m_queue.front(); 
        m_queue.pop(); 

        // return item 
        return item; 
    } 
}; 

// Driver code 
int main() 
{ 
    TSQueue<int> q; 

    // Push some data 
    q.push(1); 
    q.push(2); 
    q.push(3); 

    // Pop some data 
    std::cout << q.pop() << std::endl; 
    std::cout << q.pop() << std::endl; 
    std::cout << q.pop() << std::endl; 

    return 0; 
}

二:有超时的

来源:https://stackoverflow.com/questions/71322872/how-to-correctly-wait-for-condition-variable-timeout

while(cv.wait_for(lk, std::chrono::milliseconds(millisecond), [this]() { return !q.empty(); }) == false) {
    //超时操作
}
13 浏览
9 爬虫
0 评论