这个例子主要用到了ACE_TASK类和ACE_Thread_Manager类
#include "ace/Log_Msg.h"
#include "ace/Task.h"
#include "ace/OS.h"
#include "ace/Message_Block.h"
#include <stdio.h>
#include <string.h>
typedef struct User_
{
int len;
char buffer[20];
int count;
} User;
//The Consumer Task.
class Consumer:
public ACE_Task<ACE_MT_SYNCH>
{
public:
int open(void*)
{
ACE_DEBUG((LM_DEBUG, "(%t) Consumer task opened /n"));
//Activate the Task
activate(THR_NEW_LWP,1);
return 0;
}
//The Service Processing routine
int svc(void)
{
//Get ready to receive message from Producer
ACE_Message_Block * mb =0;
int count = 0;
do
{
mb=0;
//Get message from underlying queue
getq(mb);
User *user = (User*)mb->rd_ptr();
count = user->count;
ACE_DEBUG((LM_DEBUG,
"(%t)Got message: %s,%d,%d from remote task/n",user->buffer,user->len,user->count));
delete user;
mb->release();
}while(count < 10);
return 0;
}
int close(u_long)
{
ACE_DEBUG((LM_DEBUG,"Consumer closes down /n"));
return 0;
}
};
class Producer : public ACE_Task<ACE_MT_SYNCH>
{
public:
Producer(Consumer * consumer):consumer_(consumer), count(0)
{
//mb_=new ACE_Message_Block((char*)&data_,sizeof(data_));
}
int open(void*)
{
ACE_DEBUG((LM_DEBUG, "(%t) Producer task opened /n"));
//Activate the Task
activate(THR_NEW_LWP,1);
return 0;
}
//The Service Processing routine
int svc(void)
{
while(count<11)
{
//Send message to consumer
ACE_DEBUG((LM_DEBUG,
"(%t)Sending message: %d to remote task/n",count));
User *user = new User();
sprintf(user->buffer,"hello %d",count);
user->len = strlen(user->buffer);
user->count = count;
mb_=new ACE_Message_Block((char*)user,sizeof(User));
consumer_->putq(mb_);
//Go to sleep for a sec.
ACE_OS::sleep(1);
count++;
}
return 0;
}
int close(u_long)
{
ACE_DEBUG((LM_DEBUG,"Producer closes down /n"));
return 0;
}
private:
int count;
Consumer * consumer_;
ACE_Message_Block * mb_;
};
int main(int argc, char * argv[])
{
Consumer * consumer = new Consumer;
Producer * producer = new Producer(consumer);
producer->open(0);
consumer->open(0);
//Wait for all the tasks to exit.
ACE_Thread_Manager::instance()->wait();
ACE_OS::system("pause");
delete producer;
delete consumer;
return 0;
}