找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4211|回复: 1

ACE_TASK 使用问题

[复制链接]
发表于 2008-5-21 23:13:23 | 显示全部楼层 |阅读模式
仿书上的例子,写了个ACE_TASK 类应用的例子,不过老出问题,说是内存非法引用。各位帮忙看看:
realtrans.h
#include "ace/OS.h"
#include "ace/Task.h"
#include <iostream>
class Manager : public ACE_Task<ACE_MT_SYNCH>
{
public:
enum { POOLSIZE = 3 } ;
Manager() { }
int open(void*) ;
unsigned long close() ;
int svc() ;
private:
//Worker *worker_ ;
} ;
class Worker : public ACE_Task<ACE_MT_SYNCH>
{
public:

Worker() : count_( 0 )
{
}
int svc() ;
int close()
{
  return 0 ;
}
private:
ACE_Thread_Mutex mutex_ ;
int count_ ;
} ;

RealTrans.cpp
#include "RealTrans.h"
int Manager::open( void * )
{
activate() ;
return 0 ;
}
unsigned long Manager::close()
{
return 0 ;
}
int Manager::svc()
{
Worker pool ;
pool.activate( THR_NEW_LWP | THR_JOINABLE, POOLSIZE ) ;

for( int i = 0 ; i < 10; ++i )
{  
  ACE_Message_Block *mb = 0;
  ACE_NEW_RETURN( mb,
      ACE_Message_Block(strlen("This is message 1\n")),
      -1);
  //Insert data into the message block using the wr_ptr
  sprintf(mb->wr_ptr(), "This is message %d\n", i);

  //Be careful to advance the wr_ptr by the necessary amount.
  //Note that the argument is of type "size_t" that is mapped to
  //bytes.
  mb->wr_ptr(strlen("This is message 1\n"));
  pool.putq( mb ) ;
}
return 0 ;
}
int Worker::svc( void )
{
while( count_ < 10 )
{
    ACE_Message_Block *mb = 0 ;
    if( this->getq( mb ) == -1 )
    {
     std::cout << "Shutting down" << '\n' ;
     break ;
   }
  
   this->mutex_.acquire() ;  
   ++count_ ;
   ACE_thread_t  threadid = ACE_Thread::self() ;
   std::cout << threadid << '\n' ;
   std::cout << " count_ = " << count_ << '\n' ;
   std::cout << mb->rd_ptr() << '\n' ;
   mb->release();
   this->mutex_.release() ;
}
return 0 ;
}
int main( int argc , char * argv[] )
{
Manager  mgr ;
mgr.open(0) ;
ACE_Thread_Manager::instance()->wait();
return 0 ;
}
 楼主| 发表于 2008-5-21 23:13:43 | 显示全部楼层
对象管理不当,检查线程同步。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-11-22 01:36 , Processed in 0.024360 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表