一个Windows C++的线程类实现
2014-11-13 13:55
363 查看
CThread的头文件:
CThread的cpp文件:
测试程序:
#ifndef __C_PLUS_THREAD_HEADER #define __C_PLUS_THREAD_HEADER #include <string> #include <windows.h> #include <process.h> using namespace std; class Runnable { public: virtual ~Runnable(){}; virtual void Run()=0; }; class CThread:public Runnable { private: explicit CThread(const CThread &rhs); public: CThread(); CThread(Runnable* pRunnable); CThread(const char* ThreadName, Runnable* pRunnable=NULL); CThread(std::string ThreadName, Runnable* pRunnable=NULL); ~CThread(); // 开始运行线程 BOOL Start(BOOL bSuspend=FALSE); // 运行线程虚函数,派生类重写该函数 virtual void Run(); //当前执行此函数线程等待线程结束 //timeout 等待超时时间,如果为负数,等待无限时长 void Join(int timeout =-1); void Resume(); void Suspend(); BOOL Terminate(unsigned long Exitcode); unsigned int GetThreadID(); string GetThreadName(); void SetThreadName(std::string ThreadName); void SetThreadName(const char* ThreadName); private: static unsigned int WINAPI StaticThreadFunc(void* argv); private: HANDLE m_handle; Runnable* const m_pRunnable; unsigned int m_ThreadID; std::string m_ThreadName; volatile BOOL m_bRun; }; #endif
CThread的cpp文件:
// CThread.cpp: implementation of the CThread class. #include "CThread.h" CThread::CThread():m_pRunnable(NULL),m_bRun(FALSE) { } CThread::~CThread() { } CThread::CThread(Runnable* pRunnable):m_ThreadName(""),m_pRunnable(pRunnable),m_bRun(FALSE) { } CThread::CThread(const char* ThreadName,Runnable* pRunnable):m_ThreadName(ThreadName),m_pRunnable(pRunnable),m_bRun(FALSE) { } CThread::CThread(std::string ThreadName,Runnable* pRunnable):m_ThreadName(ThreadName),m_pRunnable(pRunnable),m_bRun(FALSE) { } BOOL CThread::Start(BOOL bSuspend) { if ( m_bRun) { return TRUE; } if (bSuspend) { m_handle = (HANDLE)_beginthreadex(NULL,0,StaticThreadFunc,this,CREATE_SUSPENDED,&m_ThreadID); } else { m_handle = (HANDLE)_beginthreadex(NULL,0, StaticThreadFunc,this,0, &m_ThreadID); } m_bRun = (NULL!=m_handle); return m_bRun; } void CThread::Run() { if (!m_bRun) { return; } if (NULL!=m_pRunnable) { m_pRunnable->Run(); } m_bRun = FALSE; } void CThread::Join(int timeout) { if (NULL==m_handle ||!m_bRun) { return; } if (timeout<0) { timeout = INFINITE; } ::WaitForSingleObject(m_handle, timeout); } void CThread::Resume() { if (NULL==m_handle ||!m_bRun) { return; } ::ResumeThread(m_handle); } void CThread::Suspend() { if (NULL==m_handle ||!m_bRun) { return; } ::SuspendThread(m_handle); } BOOL CThread::Terminate(unsigned long Exitcode) { if ( NULL==m_handle||!m_bRun) { return TRUE; } if (::TerminateThread(m_handle,Exitcode)) { ::CloseHandle(m_handle); return TRUE; } return FALSE; } unsigned int CThread::GetThreadID() { return m_ThreadID; } std::string CThread::GetThreadName() { return m_ThreadName; } void CThread::SetThreadName(const char* ThreadName) { if (NULL == ThreadName) { m_ThreadName=""; } else { m_ThreadName= ThreadName; } } void CThread::SetThreadName(std::string ThreadName) { m_ThreadName = ThreadName; } unsigned int CThread::StaticThreadFunc(void* argv) { CThread* pThread = (CThread*)argv; pThread->Run(); return 0; }
测试程序:
#include <stdio.h> #include "CThread.h" #include "CThreadPoolExecutor.h" class Example:public Runnable { public: ~Example() { printf("Example Destruction\n"); } void Run() { printf("Hello CThread\n"); } }; int main(int argc, char* argvp[]) { CThreadPoolExecutor* pExecutor = new CThreadPoolExecutor(); pExecutor->Init(2,10,50); Example exam; CThread * t = NULL; t = new CThread(&exam); t->Start(); t->Join(); getchar(); return 0; }
相关文章推荐
- Cpp-一个Windows C++的线程类实现
- 一个Windows C++的线程类实现
- 一个Windows C++的线程类实现
- 一个基于Loki::SingletonHolder的Windows线程安全的C++日志类实现
- C++实现一个简单的双线程MVC框架
- C++实现线程同步的几种方式 线程同步是指同一进程中的多个线程互相协调工作从而达到一致性。之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏,下面是多
- windows C++ 获得一个进程的线程数目
- Windows Via C/C++:线程实现细节
- 一个Windows C++的线程池类实现
- C++实现一个线程安全的单例工厂实现代码
- 一个Windows C++的线程池的实现
- 线程实现定时器(windows下C++版)
- C++实现一个线程安全的单例工厂
- 一个Windows C++的线程池的实现
- C++实现一个线程安全且高效单例类。(懒汉与饿汉)
- C++实现一个线程安全且高效单例类
- 一个简单的 websocket的 c++ 服务器 实现windows
- 多线程程序设计之创建线程(Windows下C++实现)
- windows平台下Gearman的一个Worker和Client实例,C++实现
- 一个Windows C++ 读写锁的实现