|
楼主 |
发表于 2007-12-19 22:01:06
|
显示全部楼层
全部代码如下:- #include "ace/OS.h"
- #include "ace/Task.h"
- #include "ace/Message_Block.h"
- //The Consumer Task.
- class Consumer : public ACE_Task<ACE_MT_SYNCH>
- {
- public:
- int open(void*)
- {
- ACE_DEBUG((LM_DEBUG, "(%t) Producer task opened \n"));
- this->activate();
- return 0;
- }
- //The Service Processing routine
- int svc(void)
- {
- int ret = 0;
- //Get ready to receive message from Producer
- ACE_Message_Block *mb = 0;
- while (1)
- {
- ret = this->getq(mb);
- if (ret < 0)
- ACE_DEBUG((LM_DEBUG, "(%t)error.\n"));
- ProcessTask(mb);
- mb->release();
- }
- return 0;
- }
- void ProcessTask(ACE_Message_Block *mb)
- {
- char *pBuf = mb->rd_ptr();
- ACE_DEBUG((LM_DEBUG, "(%t)Got message: '%s' from remote task\n",pBuf));
- ACE_OS::sleep(10);
- }
- 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)
- {
- }
- int open(void*)
- {
- ACE_DEBUG((LM_DEBUG, "(%t) Producer task opened \n"));
-
- this->activate();
- return 0;
- }
- //The Service Processing routine
- int svc(void)
- {
- while(count_ < 21)
- {
- char data[32] = {0};
- ACE_OS::sprintf(data, "data form Producer, NO=%d.", data_);
- ACE_NEW_RETURN(mb_, ACE_Message_Block(32, ACE_Message_Block::MB_DATA, 0, data), -1);
- mb_->wr_ptr(32);
- //Send message to consumer
- ACE_DEBUG((LM_DEBUG, "(%t)Sending message: %d to remote task\n", data_));
- 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);
- consumer->open(0);
- producer->open(0);
- //Wait for all the tasks to exit.
- ACE_Thread_Manager::instance()->wait();
- if (consumer)
- {
- delete consumer;
- consumer = NULL;
- }
- if (producer)
- {
- delete producer;
- producer = NULL;
- }
- return 0;
- }
复制代码 |
|