|
是这样,在学习ACE,刚看到多线程那块,看着例子试着写些东西做练习,现在碰到这样一个问题,我的代码在调用ACE_Thread_Manage::instance()->spawn_n的时候总会出错,调试的时候在代码ACE_OS::thread_mutex_lock()这个函数里发现m这个参数是空指针,看来看去跟例子里的调用方法好像没区别丫。代码贴出来,大家帮忙看看
另外请教一下,spawn这个函数里头那些个THR_NEW_LWP还有THR_DETACHED这些标志位什么意思?
- #include <ace/Thread_Manager.h>
- #include <ace/Synch.h>
- #include <ace/Log_Msg.h>
- struct THREADARG
- {
- public:
- THREADARG(ACE_Mutex * mutex_p)
- : mutex_(mutex_p)
- , number(0)
- {
- }
- ACE_Mutex * mutex_;
- int number;
- };
- static void * WorkThread(void * arg)
- {
- THREADARG * arg_ = (THREADARG *)arg;
- ACE_DEBUG((LM_DEBUG,"Begin thread id = %t,number = %d\n",arg_->number));
- //arg_->mutex_->acquire();
- arg_->number++;
- ACE_OS::sleep(1);
- ACE_DEBUG((LM_DEBUG,"Thread id = %t,number = %d\n",arg_->number));
- //arg_->mutex_->release();
- //arg_->mutex_->acquire();
- arg_->number++;
- ACE_OS::sleep(1);
- ACE_DEBUG((LM_DEBUG,"Thread id = %t,number = %d\n",arg_->number));
- //arg_->mutex_->release();
- ACE_DEBUG((LM_DEBUG,"Thread id = %t exit!\n"));
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- ACE_DEBUG((LM_DEBUG,"Now begin work 5 thread!\n"));
- ACE_Mutex mutex;
- THREADARG thr_arg(&mutex);
- // for (int i = 0;i < 5;i++)
- // ACE_Thread_Manager::instance()->spawn((ACE_THR_FUNC)WorkThread,&thr_arg,THR_NEW_LWP | THR_DETACHED,0,0,ACE_DEFAULT_THREAD_PRIORITY,1);
- ACE_Thread_Manager * thread_manage = ACE_Thread_Manager::instance();
- int nret = thread_manage->spawn_n(5,(ACE_THR_FUNC)WorkThread,(void *)&thr_arg,THR_NEW_LWP ,ACE_DEFAULT_THREAD_PRIORITY);
- if (nret == -1)
- ACE_DEBUG((LM_DEBUG,"Thread begin failed!\n"));
- ACE_Thread_Manager::instance()->wait_grp(nret);
- ACE_DEBUG((LM_DEBUG,"All thread exit!\n"));
- return 0;
- }
复制代码 |
|