关闭

线程消息队列 示例

标签: 消息队列 queue
163人阅读 评论(0) 收藏 举报
分类:
#include<stdio.h>

#pragma once
#ifdef LINUX
#include <pthread.h>
#else


#endif

#define APP_OK 0
#define APP_FAIL -1


#define IN_QUEUE 1
#define OUT_QUEUE 0
#define MAX_QUEUE 256


typedef struct pmessage_
{
struct pmessage_ *next;
unsigned int msg[4];//message body
int is_in_queue;
}pmessage;


class Queuemessage
{
public:
int m_maxnum;//最大消息队列数目
int m_curnum;// 当前消息数目
pthread_mutex_t  m_mutex;
pthread_cond_t  m_condQueue;
pmessage *m_head;
pmessage *m_tail;
pmessage *m_messagebody[MAX_QUEUE];

public:
Queuemessage(void);
virtual ~Threadmessage(void);
int init_messgae(int queueNo);
pmessage* get_idle_body();//获取空闲的消息体
int insert_queue(pmessage *messgae);//入列
pmessage* push_queue();// 从头部出列
// int read_one_message();//从队列读出消息
// int write_one_message();//写一个消息体
int queue_isfull();//判断当前线程是否满
int queue_isempty();//消息队列是否空的
int pthread_sleep();//阻塞当前线程
int pthread_wakeup();//唤醒线程


};



Queuemessage::Queuemessage(void)
{
}


Queuemessage::~Queuemessage(void)

{

for(int i =0;i<m_maxnum;i++)

{

if(m_messagebody[i] != NULL)

{

free((void *)m_messagebody[i]);

}

}

}


int Queuemessage::init_messgae(int queueNo)
{
m_maxnum = MAX_QUEUE;
m_curnum = 0;
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_condQueue, NULL);
m_head = NULL;
m_tail = NULL;
for(int i = 0; i < m_maxnum;i++)
{
m_messagebody[i] = malloc(sizeof(pmessage));
if(m_messagebody[i] == NULL)
{
return APP_FAIL;
}
}


return APP_OK;
}
pmessage* Queuemessage::get_idle_body()
{
for(int i = 0; i < m_maxnum;i++)
{
if(m_messagebody[i]->is_in_queue == OUT_QUEUE)
{
return m_messagebody[i];
}
}


return NULL;
}


int Queuemessage::insert_queue(pmessage *messgae)//从尾巴入列
{
if(messgae == NULL)
{
return APP_FAIL;
}
pthread_mutex_lock(&m_mutex);


if(queue_isfull())
{
pthread_mutex_unlock(&m_mutex);
return APP_FAIL;
}


if(queue_isempty())
{
m_head = m_tail = messgae;
}
else
{
m_tail ->next = messgae;
m_tail = messgae;
}
m_tail ->next = NULL;
m_curnum += 1;
messgae->is_in_queue = IN_QUEUE;
pthread_wakeup();//有消息过来 则唤醒读取消息的线程
pthread_mutex_unlock(&m_mutex);


return APP_OK;
}




pmessage* Queuemessage::push_queue();// 从头部出列
{
AGAIN:
pthread_mutex_lock(&m_mutex);
if(queue_isempty())
{
pthread_sleep();//如果队列里没有消息阻塞线程
pthread_mutex_unlock(&m_mutex);
goto AGAIN;
}
pmessage *tmp = NULL;
*tmp = m_head;
if(m_head->next)
{
m_head = tmp->next;
}
else
{
m_head = m_tail = NULL;
}


m_curnum -= 1;
tmp->is_in_queue = OUT_QUEUE;
pthread_mutex_unlock(&m_mutex);


return tmp;


}


int Queuemessage::queue_isfull()
{
if(m_maxnum == m_curnum)
{
return APP_OK;
}


return APP_FAIL;


}


int Queuemessage::queue_isempty()
{
if(m_curnum == 0)
{
return APP_OK;
}
return APP_FAIL;
}




int Queuemessage::pthread_sleep()//阻塞当前线程
{
pthread_cond_wait(&m_condQueue, &m_mutex);
return APP_OK;
}


int Queuemessage::pthread_wakeup()//唤醒线程
{
pthread_cond_broadcast(&m_condQueue);
return APP_OK;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15193次
    • 积分:452
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:45篇
    • 译文:0篇
    • 评论:4条
    最新评论