peakzhang 发表于 2008-5-21 23:13:23

ACE_TASK 使用问题

仿书上的例子,写了个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_tthreadid = 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[] )
{
Managermgr ;
mgr.open(0) ;
ACE_Thread_Manager::instance()->wait();
return 0 ;
}

peakzhang 发表于 2008-5-21 23:13:43

对象管理不当,检查线程同步。
页: [1]
查看完整版本: ACE_TASK 使用问题