std::quene使用


#include <string>
#include <queue>

#include <stdint.h>
 
typedef uint8_t E_CMD;

typedef struct
{
    E_CMD id;
    int more;
    Json::Value param;
}S_CMD;

class
CmdManager { public: CmdManager() { pthread_mutex_init(&m_cmd_lock, NULL); m_current_cmd = {0, 0, ""}; } void add_cmd(int cmd_id, int cmd_more = 0, Json::Value cmd_param = "") { S_CMD cmd; cmd.id = cmd_id; cmd.more = cmd_more; cmd.param = cmd_param; pthread_mutex_lock(&m_cmd_lock); m_cmd_queue.push(cmd); pthread_mutex_unlock(&m_cmd_lock); } int get_cmd_num() { return m_cmd_queue.size(); } void clear_cmd() { std::queue<S_CMD> empty; pthread_mutex_lock(&m_cmd_lock); m_cmd_queue.swap(empty); pthread_mutex_unlock(&m_cmd_lock); } #if 0 S_CMD fetch_first_cmd() { if(m_cmd_queue.size() == 0) { static S_CMD invalid_cmd = {0, 0, ""}; return invalid_cmd; } pthread_mutex_lock(&m_cmd_lock); m_current_cmd = m_cmd_queue.front(); m_cmd_queue.pop(); pthread_mutex_unlock(&m_cmd_lock); return m_current_cmd; } #endif S_CMD fetch_first_cmd(bool pop = true) { if(m_cmd_queue.size() == 0) { static S_CMD invalid_cmd = {0, 0, ""}; return invalid_cmd; } pthread_mutex_lock(&m_cmd_lock); m_current_cmd = m_cmd_queue.front(); if (pop) m_cmd_queue.pop(); pthread_mutex_unlock(&m_cmd_lock); return m_current_cmd; } S_CMD get_current_cmd() { return m_current_cmd; } private: std::queue<S_CMD> m_cmd_queue; S_CMD m_current_cmd; pthread_mutex_t m_cmd_lock; };

 

posted @ 2020-07-03 16:09  youlin  阅读(214)  评论(0编辑  收藏  举报