phil2360 发表于 2009-2-14 00:49:59

问个ACE_Thread_Manage的简单问题

是这样,在学习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;
}

_coco 发表于 2009-2-20 21:11:02

#include <ace/Thread_Manager.h>
#include <ace/Synch.h>
#include <ace/Log_Msg.h>

#if defined(WIN32)
#pragma comment(lib,"ACEd.lib")
#endif

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,"%t number = %d\n",arg_->number));
   
    arg_->mutex_->acquire();
    arg_->number++;
    arg_->mutex_->release();

    ACE_OS::sleep(1);

    ACE_DEBUG((LM_DEBUG,"%t number = %d\n",arg_->number));
   
    arg_->mutex_->acquire();
    arg_->number++;
    arg_->mutex_->release();

    ACE_OS::sleep(1);

    ACE_DEBUG((LM_DEBUG,"%t number = %d\n",arg_->number));
   
    ACE_DEBUG((LM_DEBUG,"%t exit!\n"));

    return 0;
}
int ACE_TMAIN(int argc, ACE_TCHAR* argv[])
{
    ACE_DEBUG((LM_DEBUG,"Now begin work 5 thread!\n"));

    ACE_Mutex mutex;
    THREADARG thr_arg(&mutex);

    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 | THR_JOINABLE );

    if (nret == -1)
      ACE_ERROR_RETURN((LM_DEBUG,"Thread begin failed!\n"), -1);

    ACE_Thread_Manager::instance()->wait_grp(nret);

    ACE_DEBUG((LM_DEBUG,"All thread exit!\n"));
   
    return 0;
}

出现错误的原因估计是main函数已经退出,但是线程还没有退出。

分离的(THR_DETACHED),ACE会在线程退出时自动清理这个线程持有的所有资源,其它线程无法查看这个线程的退出状态。
可会合的(THR_JOINABLE),主线程通过调用任务对象的wait()来等待子线程,如果你没有wait()它的话,你就可能泄漏系统资源。

关于你说的其它的标志位,你可以上google搜索一下,这方面的资源还是挺多的。

http://blog.csdn.net/xjtuse_mal/archive/2009/01/07/3726676.aspx

[ 本帖最后由 _coco 于 2009-2-20 21:12 编辑 ]

blaight 发表于 2009-2-24 11:13:42

你得使用ACE_TMAIN作为入口,完成对ACE的初始化。
页: [1]
查看完整版本: 问个ACE_Thread_Manage的简单问题