/********************************************************************/
* @file
* @author def< qq group: 324164944 >
* @blog http://www.cnblogs.com/itdef/
* @brief
/********************************************************************/
/******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include "stdafx.h" #include <iostream> #include <windows.h> using namespace std; class CThread{ public: CThread(); virtual ~CThread(); bool Start(); void Join(); static DWORD WINAPI ThreadProc( LPVOID lpParameter); virtual void Run() = 0; private: HANDLE hThread_; DWORD dwThreadId_; }; CThread::CThread(): hThread_(NULL),dwThreadId_(0) { cout << "Thread ..." << endl; } CThread::~CThread() { if(hThread_ != NULL) CloseHandle(hThread_); cout << "~Thread ..." << endl; } bool CThread::Start() { bool bRet = false; hThread_ = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc, // thread function this, // argument to thread function 0, // use default creation flags &dwThreadId_); // returns the thread identifier if(hThread_) { bRet = true; } return bRet; } void CThread::Join() { WaitForSingleObject(hThread_,3000); } DWORD CThread::ThreadProc( LPVOID lpParameter) { CThread* thread = static_cast<CThread*>(lpParameter); thread->Run(); return NULL; } class CMyThread:public CThread { public: void Run(){ cout << "my thread..." << endl;} }; int _tmain(int argc, _TCHAR* argv[]) { CMyThread thread; thread.Start(); thread.Join(); return 0; }
基类是最基本的几个元素 线程ID 创建进程的函数start 运行指定的线程函数run 以及等待函数join()
使用的时候直接继承 在run函数中执行自己想执行的线程处理即可。
基于对象则未使用继承等特性,使用bind function这对利器 来实现回调
#include <windows.h> #include <iostream> #include <boost/function.hpp> class CThread { public: typedef boost::function<void ()> ThreadFunc; explicit CThread(const ThreadFunc& func); ~CThread(); void Start(); void Join(); private: static DWORD WINAPI ThreadProc(LPVOID arg); void Run(); ThreadFunc func_; HANDLE hThread_; }; void CThread::Start() { hThread_ = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc, // thread function this, // argument to thread function 0, // use default creation flags NULL); // returns the thread identifier } CThread::CThread(const ThreadFunc& func): hThread_(NULL),func_(func) { std::cout << "CThread()..." << std::endl; } void CThread::Join() { WaitForSingleObject(hThread_,3000); } CThread::~CThread() { if(hThread_) CloseHandle(hThread_); std::cout << "~CThread()..." << std::endl; } void CThread::Run() { func_(); } DWORD CThread::ThreadProc(LPVOID arg) { CThread* thread = static_cast<CThread*>(arg); thread->Run(); return NULL; } //====================================== void ThreadFunc() { std::cout << "Enter thread function ...." << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { CThread thread(ThreadFunc); thread.Start(); thread.Join(); return 0; }