一个线程封装类

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



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/07/06/1236957.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
安全 Java 容器
线程安全的集合类
线程安全的集合类
|
1月前
|
存储 缓存 安全
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
33 0
|
15天前
|
存储 安全 Java
java多线程之原子操作类
java多线程之原子操作类
|
16天前
|
Java
Java中的多线程实现:使用Thread类与Runnable接口
【4月更文挑战第8天】本文将详细介绍Java中实现多线程的两种方法:使用Thread类和实现Runnable接口。我们将通过实例代码展示如何创建和管理线程,以及如何处理线程同步问题。最后,我们将比较这两种方法的优缺点,以帮助读者在实际开发中选择合适的多线程实现方式。
22 4
|
18天前
|
Java Spring
springboot单类集中定义线程池
该内容是关于Spring中异步任务的配置和使用步骤。首先,在启动类添加`@EnableAsync`注解开启异步支持。然后,自定义线程池类`EventThreadPool`,设置核心和最大线程数、存活时间等参数。接着,将线程池bean注入到Spring中,如`@Bean(&quot;RewardThreadPool&quot;)`。最后,在需要异步执行的方法上使用`@Async`注解,例如在一个定时任务类中,使用`@Scheduled(cron = &quot;...&quot;)`和`@Async`结合实现异步定时任务。
15 2
|
28天前
|
Linux API C++
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
51 2
|
1月前
|
安全 C++ 开发者
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
【C++多线程同步】C++多线程同步和互斥的关键:std::mutex和相关类的全面使用教程与深度解析
18 0
|
1月前
|
安全 Java Unix
【C++ 包裹类 std::thread】探索C++11 std::thread:如何使用它来创建、销毁和管理线程
【C++ 包裹类 std::thread】探索C++11 std::thread:如何使用它来创建、销毁和管理线程
36 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍
|
1月前
|
安全 Java C++
JUC(java.util.concurrent)的常见类(多线程编程常用类)
JUC(java.util.concurrent)的常见类(多线程编程常用类)

热门文章

最新文章