Example 1
Author: Ambreen Ilyas

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// This example is from the ACE Programmers Guide.
//// Chapter: "The Message Queue"
//// For details please see the guide at
//// http://www.cs.wustl.edu/~schmidt/ACE.html
//// AUTHOR: Umar Syyid (usyyid@hns.com)
//// and Ambreen Ilyas (ambreen@bitsmart.com)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Example 1
#include "ace/Message_Queue.h"
#include "ace/Get_Opt.h"
#define SIZE_BLOCK 1
#define NO_MSGS 10

class QTest{
public:
QTest():no_msgs_(NO_MSGS){
//First create a message queue of default size.
if(!(this->mq_=new ACE_Message_Queue<ACE_NULL_SYNCH> ()))
ACE_DEBUG((LM_ERROR,"Error in message queue initialization \n"));
}
int start_test(){
for(int i=0; i<no_msgs_;i++){
//create a new message block of size 1
ACE_Message_Block *mb= new ACE_Message_Block(SIZE_BLOCK);

//Insert data into the message block using the rd_ptr
*mb->wr_ptr()=i;

//Be careful to advance the wr_ptr
mb->wr_ptr(1);

//Enqueue the message block onto the message queue
if(this->mq_->enqueue_prio(mb)==-1){
ACE_DEBUG((LM_ERROR,"\nCould not enqueue on to mq!!\n"));
return -1;
}
ACE_DEBUG((LM_INFO,"EQd data: %d\n",*mb->rd_ptr()));
} //end for

//Now dequeue all the messages
this->dequeue_all();
return 0;
}

void dequeue_all(){
ACE_DEBUG((LM_INFO,"\n\nBeginning DQ \n"));
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;

//dequeue the head of the message queue until no more messages are left
for(int i=0;i<no_msgs_;i++){
mq_->dequeue_head(mb);
ACE_DEBUG((LM_INFO,"DQd data %d\n",*mb->rd_ptr()));
}
}
private:
ACE_Message_Queue<ACE_NULL_SYNCH> *mq_;
int no_msgs_;
};

int main(int argc, char* argv[]){
QTest test;
if(test.start_test()<0)
ACE_DEBUG((LM_ERROR,"Program failure \n"));
}

Next Example