关于ACE创建线程

来源:互联网 发布:tpshop分销源码 编辑:程序博客网 时间:2024/05/23 19:39

ACE_Thread::spawn_n接口创建n个线程:

if(ACE_Thread::spawn_n(threadID, //id’s for each of the threads
                                               n_threads, //number of threads to spawn
                                              (ACE_THR_FUNC)worker, //entry point for new thread
                                              &arg, //args to worker
                                              THR_JOINABLE | THR_NEW_LWP, //flags
                                              ACE_DEFAULT_THREAD_PRIORITY,
                                              0, 0, threadHandles)==-1)

ACE_Thread::spawn接口创建线程:

if(ACE_Thread::spawn((ACE_THR_FUNC)worker,
                                          (void*)&arg,THR_DETACHED|THR_NEW_LWP)==-1)


ACE线程管理类使用实例:

ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();

线程管理类的线程创建:

ACE_Thread_Manager::instance()->spawn_n(num_task_1,
                                                                                   (ACE_THR_FUNC)taskone,//Execute task one
                                                                                  0, //No arguments
                                                                                  THR_NEW_LWP, //New Light Weight Process
                                                                                  ACE_DEFAULT_THREAD_PRIORITY,
                                                                                  1)==-1) //Group ID is 1

//Wait for all tasks in grp 1 to exit

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


ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();

线程管理类的线程创建另一种形式:

int grp_id = thr_mgr->spawn_n (n_threads, ACE_THR_FUNC (worker),
                                                          (void *) n_iterations,
                                                          THR_NEW_LWP | THR_DETACHED);

挂起线程组1的所有线程;

if (thr_mgr->suspend_grp (grp_id) == -1);

启动线程组1的所有线程;

if (thr_mgr->resume_grp (grp_id) == -1);

取消线程组1的所有线程;

if (thr_mgr->cancel_grp (grp_id) == -1);

worker的函数实现:

static void *
worker (int iterations)
{
    for (int i = 0; i < iterations; i++)
    {
       if ((i % 1000) == 0)
       {
            ACE_DEBUG ((LM_DEBUG,
            "(%t) checking cancellation before iteration %d!\n", i));
           //Before doing work check if you have been canceled. If so don’t
           //do any more work.
         if (ACE_Thread_Manager::instance ()->testcancel
         (ACE_Thread::self ()) != 0)
         {
            ACE_DEBUG ((LM_DEBUG,
            "(%t) has been canceled before iteration %d!\n",i));
            break;
         }
      }
   }
return 0;
}