一个封装好的线程类

原文出处

class CThread

{

public:

        /**//**

         * Default Constructor

         */

        CThread()

        { 

            m_pThreadFunction = CThread::EntryPoint;

            m_runthread = FALSE;

        }



        /**//**

         * Default Destructor

         * also destroys the thread

         */

        ~CThread()

        {

            if ( m_hThread )

                Stop(true);                    //thread still running, so force the thread to stop!

        }

        /**//**

         * Starts the thread.

         * @param dwCreationFlags        the flags to use for creating the thread. see CreateThread() in the windows sdk.

         */

        DWORD Start(DWORD dwCreationFlags = 0)

        {

            m_runthread = true;

            m_hThread = CreateThread(NULL, 0, m_pThreadFunction, this, 0, &dwCreationFlags);

            m_dwExitCode = (DWORD)-1;



            return GetLastError();

        }



        /**//**

         * Stops the thread.

         *    

         * @param bForceKill        if true, the Thread is killed immediately

         */

        DWORD Stop ( bool bForceKill = false )

        {

            if ( m_hThread )

            {

                //尝试"温柔地"结束线程

                if (m_runthread == TRUE)

                    m_runthread = FALSE;        //first, try to stop the thread nice

                

                GetExitCodeThread(m_hThread, &m_dwExitCode);



                if ( m_dwExitCode == STILL_ACTIVE && bForceKill )

                {//强制杀死线程

                    TerminateThread(m_hThread, DWORD(-1));

                    m_hThread = NULL;

                }

            }



            return m_dwExitCode;

        }

        /**//**

         * Stops the thread. first tell the thread to stop itself and wait for the thread to stop itself.

         * if timeout occurs and the thread hasn't stopped yet, then the thread is killed.

         * @param timeout    milliseconds to wait for the thread to stop itself

         */

        DWORD Stop ( WORD timeout )

        {

            Stop(false);

            WaitForSingleObject(m_hThread, timeout);//等待一段时间

            return Stop(true);

        }



        /**//**

         * suspends the thread. i.e. the thread is halted but not killed. To start a suspended thread call Resume().

         */

        DWORD Suspend()

        {//挂起线程

            return SuspendThread(m_hThread);

        }



        /**//** 

         * resumes the thread. this method starts a created and suspended thread again.

         */

        DWORD Resume()

        {//恢复线程

            return ResumeThread(m_hThread);

        }



        /**//**

         * sets the priority of the thread.

         * @param priority    the priority. see SetThreadPriority() in windows sdk for possible values.

         * @return true if successful

         */

        BOOL SetPriority(int priority)

        {//设置线程优先级

            return SetThreadPriority(m_hThread, priority);

        }



        /**//**

         * gets the current priority value of the thread.

         * @return the current priority value

         */

        int GetPriority()

        {//获取线程优先级

            return GetThreadPriority(m_hThread);

        }



protected:



        /**//**

         * 子类应该重写此方法,这个方法是实际的工作线程函数

         */

        virtual DWORD ThreadMethod() = 0;

        

private:



        /**//**

         * DONT override this method.

         *

         * this method is the "function" used when creating the thread. it is static so that way

         * a pointer to it is available inside the class. this method calls then the virtual 

         * method of the parent class.

         */

        static DWORD WINAPI EntryPoint( LPVOID pArg)

        {

            CThread *pParent = reinterpret_cast<CThread*>(pArg);

            pParent->ThreadMethod();//多态性,调用子类的实际工作函数

            return 0;

        }

        

private:

    HANDLE    m_hThread;                    /**<Thread Handle    线程句柄

    DWORD    m_dwTID;                    ///<Thread ID    线程ID

    LPVOID    m_pParent;                    ///<this pointer of the parent CThread object

    DWORD    m_dwExitCode;                ///<Exit Code of the thread 线程退出码



protected:

    LPTHREAD_START_ROUTINE    m_pThreadFunction;    /**<工作线程指针

    BOOL    m_runthread;                ///<线程是否继续运行的标志

};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值