|
头文件:
MQ.h
#ifndef MQ_H_
#define MQ_H_
#include "ace/Message_Queue.h"
#include "ace/Task.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Connector.h"
#include "ace/Log_Msg.h"
#include "ace/Os.h"
#include "ace/Thread_Mutex.h"
#include "ace/Mutex.h"
#include "ace/Lock.h"
#include "ace/Addr.cpp"
#include "ace/Time_Value.cpp"
#include "ace/String_Base_Const.cpp"
#include "ace/OS_String.cpp"
class QTest
{
public:
QTest(int num_msgs);
int enq_msgs();
int deq_msgs();
private:
//定义消息队列
ACE_Message_Queue <ACE_NULL_SYNCH> *mq_;
//消息队列个数
int no_msgs_;
};
#endif
源文件:
MQ.cpp
#include "MQ.h"
QTest::QTest(int num_msgs) : no_msgs_(num_msgs)
{
ACE_TRACE("QTest::QTest");
//第一次创建消息队列定义大小
if (! (this->mq_ = new ACE_Message_Queue <ACE_NULL_SYNCH> ()))
{
ACE_DEBUG ((LM_ERROR, "Error in message queue initialization\n"));
}
}
int QTest::enq_msgs()
{
ACE_TRACE ("QTest::enq_msgs");
for (int i=0; i<no_msgs_; i++)
{
//为每个消息创建一个消息块
ACE_Message_Block *mb;
//初始化消息块大小
ACE_NEW_RETURN (mb, ACE_Message_Block (ACE_OS::strlen("This is message 1\n")), -1);
//把数据插入到消息块
ACE_OS::sprintf (mb->wr_ptr(), "This is message %d\n", i);
//移动消息块的指针
mb->wr_ptr (ACE_OS::strlen("This is message 1\n"));
//把消息块入队
if (this->mq_->enqueue_prio (mb) == -1)
{
ACE_DEBUG ((LM_ERROR, "\nCould not enqueue on to mq!!\n"));
return -1;
}
ACE_DEBUG ((LM_INFO, "EQ'd data: %s\n", mb->rd_ptr() ));
}
//现在打印所有的消息
this->deq_msgs();
return 0;
}
int QTest::deq_msgs()
{
ACE_TRACE ("QTest::dequeue_all");
ACE_DEBUG ((LM_INFO, "No. of Messages on Q: %d Bytes on Q: %d\n",
mq_->message_count(), mq_->message_bytes()));
ACE_Message_Block *mb;
for (int i=0; i<no_msgs_; ++i)
{
mq_->dequeue_head(mb);
ACE_DEBUG ((LM_INFO, "DQ'd data %s\n", mb->rd_ptr()));
// mb->rd_ptr(ACE_OS::strlen(mb->rd_ptr()) + 1);
mb->release(); //在这里释放的时候就抛异常了。
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
ACE_ERROR_RETURN ((LM_ERROR, "Usage %s num_msgs", argv[0]), -1);
}
QTest test (ACE_OS::atoi(argv[1]));
if (test.enq_msgs() == -1)
{
ACE_ERROR_RETURN ((LM_ERROR, "Program failure\n"), -1);
}
return 0;
}
每次在我调试时, 在mb->release(); 抛出异常了
EQ'd data: This is message 0
EQ'd data: This is message 1
EQ'd data: This is message 2
EQ'd data: This is message 3
EQ'd data: This is message 4
No. of Messages on Q: 5 Bytes on Q: 90
DQ'd data This is message 0
这是为啥啊 |
|