一个抽象的线程基类, 再来个具体的线程类并实现相关接口,再写个主函数来调用下。上代码:
Thread.h
/* Windows平台线程类实现 开发环境: Win7_x64 + VC2012 */ #ifndef __THREAD_H__ #define __THREAD_H__ #pragma once #include <string> #include <windows.h> /* 1. 线程基类, 要创建新的线程类, 只需要继承此类并实现相关接口 2. 要开启线程并运行只需要调用Start()函数 3. 未完善地方: 应该写个虚函数Stop(), 当线程过程在运行时可以设置下运行标志变量让线程 退出循环过程, 再作些清理工作, 避免暴力终止线程。 */ class CThread // 抽象的线程基类 { public: CThread(const std::string threadName = "noNamed"); virtual ~CThread(); virtual void Run() = 0; // 线程执行过程 virtual bool Start(bool bSuspended/* = false*/); void Join(int timeout = -1); // 等待超时时间为负时, 表示无限等待 void Resume(); // 恢复挂起的线程 void Suspend(); // 挂起线程 bool Terminate(unsigned long exitCode); // 结束线程 unsigned int GetThreadID(); // 获取线程ID std::string GetThreadName(); void SetThreadName(std::string threadName); private: bool CreateThread(bool bSuspended = false);// 开始运行线程 static unsigned int WINAPI StaticThreadFunc(void* arg); // 线程函数 protected: HANDLE m_handle; std::string m_threadName; unsigned int m_threadID; volatile bool m_bRun; // 表明线程是否已成功创建(实际上与m_handle含义相同了) }; #endif
Thread.cpp:
#include <iostream> #include <process.h> #include "Thread.h" CThread::CThread(const std::string threadName) : m_threadName(threadName), m_threadID(0), m_bRun(false) { } CThread::~CThread() { printf("~CThread()\n"); } bool CThread::Start(bool bSuspend/* = false*/) // 创建线程并运行(默认)或挂起 { m_bRun = CreateThread(bSuspend); return m_bRun; } bool CThread::CreateThread(bool bSuspend/* = false*/) // 创建线程并运行(默认)或挂起 { if(!m_bRun) { 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::Join(int timeout/* = -1*/) // 等待超时时间(毫秒)为负时, 表示无限等待 { if(m_handle && m_bRun) { if(timeout < 0) timeout = INFINITE; ::WaitForSingleObject(m_handle, timeout); } } void CThread::Resume() // 恢复挂起的线程 { if(m_handle && m_bRun) ::ResumeThread(m_handle); } void CThread::Suspend() // 挂起线程 { if(m_handle && m_bRun) ::SuspendThread(m_handle); } bool CThread::Terminate(unsigned long exitCode) // 结束线程 { if(m_handle && m_bRun) { if(::TerminateThread(m_handle, exitCode)) { ::CloseHandle(m_handle); m_handle = NULL; m_bRun = false; return true; } } return false; } unsigned int CThread::GetThreadID() { return m_threadID; } std::string CThread::GetThreadName() { return m_threadName; } void CThread::SetThreadName(std::string threadName) { m_threadName = threadName; } unsigned int CThread::StaticThreadFunc(void* arg) // 线程函数 { CThread* pThread = (CThread*)arg; // 取得线程类指针 pThread->Run(); // 执行线程过程函数 return 0; }
Thread1.h
#ifndef __THREAD1_H__ #define __THREAD1_H__ #pragma once #include "Thread.h" /* 1. 要创建一个新线程类时只需要继承CThread, 然后在Run()中实现自己的线程过程(Run()) */ class CThread1: public CThread // 线程类1 { public: CThread1(const std::string threadName = "noNamed"); virtual ~CThread1(void); bool Start(bool bSuspended/* = false*/); virtual void Run(); }; #endif
Thread1.cpp
#include <iostream> #include "Thread1.h" CThread1::CThread1(const std::string threadName): CThread(threadName) { } CThread1::~CThread1() { printf("~CThread1()\n"); } bool CThread1::Start(bool bSuspended/* = false*/) { // todo: 此处可添加一些初始化代码 return CThread::Start(bSuspended); } void CThread1::Run() { int cnt = 0; while(cnt <= 10) { std::cout << "Hello " << m_threadName << "::Run(): " << cnt++ << std::endl; Sleep(200); } }
main.cpp
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include "Thread1.h" #define N 15 int main(int argc, char* argv[]) { char buf[20] = {0}; CThread* t[N] = {NULL}; for(int i = 0; i < N; i++) { sprintf(buf, "Thread%d", i+1); t[i] = new CThread1(buf); t[i]->Start(true); std::cout << t[i]->GetThreadName() << ": " << t[i]->GetThreadID() << std::endl; t[i]->Resume(); } for(int i = 0; i < N; i++) t[i]->Join(); return 0; }