|
用ACE_Task<ACE_MT_SYNCH>写的一个简单的多线程程序,main函数在退出时程序崩溃,但不是每次都奔溃,时不时这样,为什么?
gdb core文件,bt信息如下:
#0 0x00000035d0407aa8 in pthread_join () from /lib64/libpthread.so.0
#1 0x00000035d312e50a in ACE_5_6_1::ACE_Thread_Manager::wait(ACE_5_6_1::ACE_Time_Value const*, bool, bool) () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
#2 0x00000035d312e5e9 in ACE_5_6_1::ACE_Thread_Manager::close() () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
#3 0x00000035d312e860 in ACE_5_6_1::ACE_Thread_Manager::close_singleton() () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
#4 0x00000035d30f9a0a in ACE_5_6_1::ACE_Object_Manager::fini() () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
#5 0x00000035d30f9b18 in ACE_5_6_1::ACE_Object_Manager::~ACE_Object_Manager() () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
#6 0x00000035d30f9d03 in ACE_5_6_1::ACE_Object_Manager_Manager::~ACE_Object_Manager_Manager() () from /usr/lib64/ACE-5.6.1/libACE.so.5.6.1
困扰了好几天了,百思不得其解,有没有高人指点下呀,小女子不胜感激!
代码大致如下
class Mytask: public ACE_Task<ACE_MT_SYNCH>{
public:
Mytask(int num):m_num(0),m_stop(false),_thr_count(num){
size_t queue_size=1024;
req_queue_.high_water_mark(queue_size*sizeof(iovec));
req_queue_.low_water_mark(queue_size*sizeof(iovec)/3);
init();
open();
}
~Mytask(){
for (int i=0;i<_thr_count;i++)
delete []_stack;
delete []_stack;
delete []_stack_size;
}
int init(){
water_marks(ACE_IO_Cntl_Msg::SET_HWM,1000);
return 0;
}
int open(){
_stack_size = new size_t[_thr_count];
_stack = new char*[_thr_count];
for(int i=0;i<_thr_count;i++){
_stack = new char[2*1024*1024];
_stack_size = 2*1024*1024;
}
int ret = activate(THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, _thr_count,0,ACE_DEFAULT_THREAD_PRIORITY,-1,0,0,(void**)_stack,_stack_size);
return ret;
}
int stop(){
m_stop = true;
req_queue_.deactivate();
req_queue_.close();
fprintf(stderr,"at stop() end\n");
return 0;
}
int svc(){
while(!m_stop){
fprintf(stderr,"%d\n",m_num);
m_num++;
}
fprintf(stderr,"at end of svc(),m_num=%d\n",m_num);
return 0;
}
int m_num;
bool m_stop;
int _thr_count;
protected:
ACE_Message_Queue_Ex<iovec, ACE_MT_SYNCH> req_queue_;
char **_stack;
size_t *_stack_size;
};
int main(int argc, char * argv[])
{
Mytask mytask(3);
sleep(2);
mytask.stop();
mytask.wait();
fprintf(stderr,"at end of main,m_num=%d\n",mytask.m_num);
return 0;
}
|
|