一个封装好的线程类
来源:互联网 发布:linux vim 显示行数 编辑:程序博客网 时间:2024/06/05 07:47
原文出处
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
- 一个封装好的线程类
- 一个封装好的线程类
- 好用的win32线程封装类
- 一个封装好的串口类
- 一个封装好的 FileSystemWatcher 类
- 一个封装好的nofication类
- 一个好用的 poi封装类
- 一个线程封装类
- 封装一个简单的windows线程类
- C++封装一个简单的线程类
- C++封装一个简单的线程类
- 一个封装好的Asp.Net C# HttpRequest请求类
- 查到一个好用的汉字转拼音封装类
- 一个封装好的Slf4jLogUtil工具类(下)
- 封装一个超级好用的ToastUtils帮助类
- 封装一个Window线程类
- 封装好的一个数据接口
- 一个简单封装好的item
- Mahout中数据的存储方式
- 基于Mahout的电影推荐系统
- 贝叶斯理论——一种由表及里的方法
- vs2010+Qt5.5.0开发环境搭建
- Elasticsearch做"关联查询"——nested-parent关系
- 一个封装好的线程类
- 使用Mahout实现协同过滤 spark
- GitHub 优秀的 Android 开源项目
- Hi35xx NVR GDB调试
- 15电气郄慧敏vb作业1在VB中制作计时器
- [置顶] Struts2 的token原理
- 下载网络文件HttpURLConnection.getContentLength()大小为 0
- 约瑟夫环的一种简单解法
- [置顶] SpringSecurity 源码分析一