一个线程封装类

复制代码
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, this0&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;                ///<线程是否继续运行的标志
};

复制代码

    子类只需要从此类继承并重写ThreadMethod()方法就可以了。

posted on   Phinecos(洞庭散人)  阅读(2556)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2007-07-06 SWT Designer 6.0安装小记

导航

统计

点击右上角即可分享
微信分享提示