ACE Thread Manage

来源:互联网 发布:软件开发设计培训 编辑:程序博客网 时间:2024/05/21 17:49
 
线程类型
Thread Creation Flags

Flag

Description

THR_CANCEL_DISABLE

Do not allow this thread to be canceled.

THR_CANCEL_ENABLE

Allow this thread to be canceled.

THR_CANCEL_DEFERRED

Allow for deferred cancellation only.

THR_BOUND

Create a thread that is bound to a kernel-schedulable entity.

THR_NEW_LWP

Create a kernel-level thread.

THR_DETACHED

Create a detached thread.

THR_SUSPENDED

Create the thread but keep the new thread in suspended state.

THR_DAEMON

Create a daemon thread.

THR_JOINABLE

Allow the new thread to be joined with.

THR_SCHED_FIFO

Schedule the new thread using the FIFO policy, if available.

THR_SCHED_RR

Schedule the new thread using a round-robin scheme, if available.

THR_SCHED_DEFAULT

Use whatever default scheduling scheme is available on the operating system.


handler.activate (THR_NEW_LWP |        //内核调度的线程
THR_SCHED_DEFAULT | //默认调度方案
THR_SUSPENDED | //开始挂起,可用resume()唤醒
THR_JOINABLE); //可以会合的
Kernel- and User-Level Threads
ACE_Task_Base::activate() 方法默认行为是的新的用户级线程被绑定到一个新的内核级(THR_NEW_LWP标志),但是如果你指定了自己的属性标志参数,你需要自己去包含THR_NEW_LWP标志。
分离的(
Detached )和可会合的(Joinable )线程
分离的(THR_DETACHED),ACE会在线程退出时自动清理这个线程持有的所有资源,其它线程无法查看这个线程的退出状态。
可会合的(THR_JOINABLE),主线程通过调用任务对象的wait()来等待子线程,如果你没有wait()它的话,你就可能泄漏系统资源。
线程的优先级
每个OS都会以自己的方式定义优先级:
Solaris:0~127(最高);
Windows:0~15(最高);
VxWorks:0~255(最低)。
Thread Priority Macros

Macro

Meaning

ACE_THR_PRI_OTHER_MIN

Minimum priority for the time-shared scheduling class

ACE_THR_PRI_OTHER_DEF

Default priority for the time-shared scheduling class

ACE_THR_PRI_OTHER_MAX

Maximum priority for the time-shared scheduling class

ACE_THR_PRI_RR_MIN

Minimum priority for the real-time scheduling class with the round-robin policy

ACE_THR_PRI_RR_DEF

Default priority for the real-time scheduling class with the round-robin policy

ACE_THR_PRI_RR_MAX

Maximum priority for the real-time scheduling class with the round-robin policy

ACE_THR_PRI_FIFO_MIN

Minimum priority for the real-time scheduling class with the FIFO policy

ACE_THR_PRI_FIFO_DEF

Default priority for the real-time scheduling class with the FIFO policy

ACE_THR_PRI_FIFO_MAX

Maximum priority for the real-time scheduling class with the FIFO policy

这些宏需要你根据你的平台去定义的,更细致的控制需要ACE_Sched_Params
一次创建多个相同线程
// Create 4 threads.
handler.activate (THR_NEW_LWP | THR_JOINABLE, 4);
handler.wait ();


使用ACE_Thread_Manager进行线程管理
这个管理器(ACE_Thread_Manager)提供了一个丰富的接口,组管理、创建、状态获取、线程启动挂钩和退出挂钩,and so on。
线程退出挂钩(“最后一秒”的清理)
它总是会被调用即使你调用了低级的ACE_Thread::exit()突然退出。
只需要创建
ACE_At_Thread_Exit的之类,重载apply()虚方法(do something you want),然后向ACE_Thread_Manager登记这个类的一个实例。
#include <ace/Task.h>
#include <ace/Log_Msg.h>

class ExitHandler : public ACE_At_Thread_Exit
{
public:
virtual void apply (void)
{
    ACE_DEBUG((LM_INFO,
               ACE_TEXT ("(%t) is exiting /n")));

    // Shut down all devices.
}
};
class HA_CommandHandler : public ACE_Task_Base
{
private:
    ExitHandler& eh_;
public:
    HA_CommandHandler(ExitHandler& eh) : eh_(eh)
    { }
    virtual int svc (void)
    {
        ACE_DEBUG((LM_DEBUG,
                   ACE_TEXT("(%t) starting up /n")));

        this->thr_mgr()->at_exit(eh_);

        // Do something.

        // Forcefully exit.
        ACE_Thread::exit();

        // NOT REACHED
        return 0;
    }

};

int ACE_TMAIN (int, ACE_TCHAR *[])
{
    ExitHandler eh;
    ACE_Thread_Manager tm;

    HA_CommandHandler handler(eh);
    handler.thr_mgr(&tm);
    handler.activate();

    tm.wait();
    return 0;
}

线程启动挂钩
ACE提供了一个全局的启动挂钩,可用于拦截对线程启动函数的调用(所有线程)。
ACE_Thread_Hook派生一个子类,重载start()虚方法。
#include <ace/Log_Msg.h>
#include <ace/Thread_Hook.h>
#include <ace/Task.h>

class HA_ThreadHook : public ACE_Thread_Hook
{
public:
    virtual ACE_THR_FUNC_RETURN start(ACE_THR_FUNC func, void* arg)
    {
        ACE_DEBUG((LM_DEBUG,
                   ACE_TEXT("(%t) New Thread Spawned/n")));

        //// Create the context on the thread's own stack.
        //ACE_TSS<SecurityContext> secCtx;
        //// Special initialization.
        //add_sec_context_thr(secCtx);

        return (*func)(arg);
    }

    //void add_sec_context_thr(ACE_TSS<SecurityContext> &secCtx);
};

int ACE_TMAIN(int, ACE_TCHAR *[])
{
    HA_ThreadHook hook;
    ACE_Thread_Hook::thread_hook(&hook);

    ACE_Task_Base handler;
    handler.activate();
    handler.wait();
    return 0;
}
信号(Signals)
由于在Windows平台上面,略。
取消(Cancellation)
取消(Cancellation)是一种你可以用消灭(zap)正在运行的线程的途经。任何线程退出处理器都不会被调用;线程专有存储(thread-specific storage )也不会被释放。
除了协作式取消(cooperative cancellation),你最好避免使用取消功能,除非你认为真的有必要使用他们。
取消的若干模式:
1)延迟的取消(Deferred cancelability)
2)
协作式取消(cooperative cancellation)
线程并没有真的被取消,而是会在派生它们的ACE_Thread_Manager 实例中被标记为已取消,你可以调用ACE_Thread_Manager::testcancel() 来确定线程是否处在已取消状态,----如果是这样,你可以选择退出线程。
3)异步取消(Asynchronous cancelability)
4)禁用(
Disabled

ACE_Thread::disablecancel() 可以完全禁止取消某个线程。

//协作式取消(cooperative cancellation)
#include <ace/OS.h>
#include <ace/Log_Msg.h>
#include <ace/Task.h>
#include <ace/Thread_Manager.h>

class CanceledTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
    virtual int svc(void)
    {
        ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%t) starting up /n")));
        // Cache our ACE_Thread_Manager pointer.
        ACE_Thread_Manager* mgr = this->thr_mgr();
        while (1)
        {
            if (mgr->testcancel(mgr->thr_self()))
                return 0;

            ACE_Message_Block* mb;
            ACE_Time_Value tv(0, 1000);
            tv += ACE_OS::time(0);
            int result = this->getq(mb, &tv);
            if (result == -1 && errno == EWOULDBLOCK)
                continue;
            else
            {
                // Do real work.
            }
        }
        return 0;
    }
};

int ACE_TMAIN(int, ACE_TCHAR *[])
{
    CanceledTask task;
    task.activate();

    ACE_OS::sleep(1);

    ACE_Thread_Manager::instance()->cancel_task(&task);
    task.wait();
    return 0;
}
原创粉丝点击