Thread.h
1 #ifndef __THREAD_H__ 2 #define __THREAD_H__ 3 4 #include <string> 5 6 #include <windows.h> 7 #include <process.h> 8 9 class Runnable 10 { 11 public: 12 virtual ~Runnable() {}; 13 virtual void Run() = 0; 14 }; 15 16 class CThread : public Runnable 17 { 18 private: 19 explicit CThread(const CThread & rhs); 20 21 public: 22 CThread(); 23 CThread(Runnable * pRunnable); 24 CThread(const char * ThreadName, Runnable * pRunnable = NULL); 25 CThread(std::string ThreadName, Runnable * pRunnable = NULL); 26 ~CThread(void); 27 28 /** 29 开始运行线程 30 @arg bSuspend 开始运行时是否挂起 31 **/ 32 bool Start(bool bSuspend = false); 33 34 /** 35 运行的线程函数,可以使用派生类重写此函数 36 **/ 37 virtual void Run(); 38 39 /** 40 当前执行此函数线程等待线程结束 41 @arg timeout 等待超时时间,如果为负数,等待无限时长 42 **/ 43 void Join(int timeout = -1); 44 /** 45 恢复挂起的线程 46 **/ 47 void Resume(); 48 /** 49 挂起线程 50 **/ 51 void Suspend(); 52 /** 53 终止线程的执行 54 **/ 55 bool Terminate(unsigned long ExitCode); 56 57 unsigned int GetThreadID(); 58 std::string GetThreadName(); 59 void SetThreadName(std::string ThreadName); 60 void SetThreadName(const char * ThreadName); 61 62 private: 63 static unsigned int WINAPI StaticThreadFunc(void * arg); 64 65 private: 66 HANDLE m_handle; 67 Runnable * const m_pRunnable; 68 unsigned int m_ThreadID; 69 std::string m_ThreadName; 70 volatile bool m_bRun; 71 }; 72 73 #endif
Thread.cpp
1 #include "stdafx.h" 2 #include "Thread.h" 3 4 CThread::CThread(void) : 5 m_pRunnable(NULL), 6 m_bRun(false) 7 { 8 } 9 10 CThread::~CThread(void) 11 { 12 } 13 14 CThread::CThread(Runnable * pRunnable) : 15 m_ThreadName(""), 16 m_pRunnable(pRunnable), 17 m_bRun(false) 18 { 19 } 20 21 CThread::CThread(const char * ThreadName, Runnable * pRunnable) : 22 m_ThreadName(ThreadName), 23 m_pRunnable(pRunnable), 24 m_bRun(false) 25 { 26 } 27 28 CThread::CThread(std::string ThreadName, Runnable * pRunnable) : 29 m_ThreadName(ThreadName), 30 m_pRunnable(pRunnable), 31 m_bRun(false) 32 { 33 } 34 35 bool CThread::Start(bool bSuspend) 36 { 37 if(m_bRun) 38 { 39 return true; 40 } 41 if(bSuspend) 42 { 43 m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, CREATE_SUSPENDED, &m_ThreadID); 44 } 45 else 46 { 47 m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_ThreadID); 48 } 49 m_bRun = (NULL != m_handle); 50 return m_bRun; 51 } 52 53 void CThread::Run() 54 { 55 if(!m_bRun) 56 { 57 return; 58 } 59 if(NULL != m_pRunnable) 60 { 61 m_pRunnable->Run(); 62 } 63 m_bRun = false; 64 } 65 66 void CThread::Join(int timeout) 67 { 68 if(NULL == m_handle || !m_bRun) 69 { 70 return; 71 } 72 if(timeout <= 0) 73 { 74 timeout = INFINITE; 75 } 76 ::WaitForSingleObject(m_handle, timeout); 77 } 78 79 void CThread::Resume() 80 { 81 if(NULL == m_handle || !m_bRun) 82 { 83 return; 84 } 85 ::ResumeThread(m_handle); 86 } 87 88 void CThread::Suspend() 89 { 90 if(NULL == m_handle || !m_bRun) 91 { 92 return; 93 } 94 ::SuspendThread(m_handle); 95 } 96 97 bool CThread::Terminate(unsigned long ExitCode) 98 { 99 if(NULL == m_handle || !m_bRun) 100 { 101 return true; 102 } 103 if(::TerminateThread(m_handle, ExitCode)) 104 { 105 ::CloseHandle(m_handle); 106 return true; 107 } 108 return false; 109 } 110 111 unsigned int CThread::GetThreadID() 112 { 113 return m_ThreadID; 114 } 115 116 std::string CThread::GetThreadName() 117 { 118 return m_ThreadName; 119 } 120 121 void CThread::SetThreadName(std::string ThreadName) 122 { 123 m_ThreadName = ThreadName; 124 } 125 126 void CThread::SetThreadName(const char * ThreadName) 127 { 128 if(NULL == ThreadName) 129 { 130 m_ThreadName = ""; 131 } 132 else 133 { 134 m_ThreadName = ThreadName; 135 } 136 } 137 138 unsigned int CThread::StaticThreadFunc(void * arg) 139 { 140 CThread * pThread = (CThread *)arg; 141 pThread->Run(); 142 return 0; 143 }
调用:
1 #include "stdafx.h" 2 #include "Thread.h" 3 4 class R : public Runnable 5 { 6 public: 7 ~R() 8 { 9 printf("~R/n"); 10 } 11 void Run() 12 { 13 printf("Hello World/n"); 14 } 15 }; 16 17 18 int _tmain(int argc, _TCHAR* argv[]) 19 { 20 R r; 21 CThread * t = NULL; 22 t = new CThread(&r); 23 t->Start(); 24 t->Join(); 25 getchar(); 26 return 0; 27 }
from:http://blog.youkuaiyun.com/huyiyang2010/article/details/5801597