一个Windows C++的线程类实现

简介: Thread.h   [cpp] view plaincopy #ifndef __THREAD_H__  #define __THREAD_H__    #include     #include     #include       class R...

Thread.h

 

[cpp]   view plain copy
  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

 

[cpp]   view plain copy
  1. #include "Thread.h"  
  2.   
  3. CThread::CThread(void) :   
  4. m_pRunnable(NULL),  
  5. m_bRun(false)  
  6. {  
  7. }  
  8.   
  9. CThread::~CThread(void)  
  10. {  
  11. }  
  12.   
  13. CThread::CThread(Runnable * pRunnable) :   
  14. m_ThreadName(""),  
  15. m_pRunnable(pRunnable),  
  16. m_bRun(false)  
  17. {  
  18. }  
  19.   
  20. CThread::CThread(const char * ThreadName, Runnable * pRunnable) :   
  21. m_ThreadName(ThreadName),  
  22. m_pRunnable(pRunnable),  
  23. m_bRun(false)  
  24. {  
  25. }  
  26.   
  27. CThread::CThread(std::string ThreadName, Runnable * pRunnable) :   
  28. m_ThreadName(ThreadName),  
  29. m_pRunnable(pRunnable),  
  30. m_bRun(false)  
  31. {  
  32. }  
  33.   
  34. bool CThread::Start(bool bSuspend)  
  35. {  
  36.     if(m_bRun)  
  37.     {  
  38.         return true;  
  39.     }  
  40.     if(bSuspend)  
  41.     {  
  42.         m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, CREATE_SUSPENDED, &m_ThreadID);  
  43.     }  
  44.     else  
  45.     {  
  46.         m_handle = (HANDLE)_beginthreadex(NULL, 0, StaticThreadFunc, this, 0, &m_ThreadID);  
  47.     }  
  48.     m_bRun = (NULL != m_handle);  
  49.     return m_bRun;  
  50. }  
  51.   
  52. void CThread::Run()  
  53. {  
  54.     if(!m_bRun)  
  55.     {  
  56.         return;  
  57.     }  
  58.     if(NULL != m_pRunnable)  
  59.     {  
  60.         m_pRunnable->Run();  
  61.     }  
  62.     m_bRun = false;  
  63. }  
  64.   
  65. void CThread::Join(int timeout)  
  66. {  
  67.     if(NULL == m_handle || !m_bRun)  
  68.     {  
  69.         return;  
  70.     }  
  71.     if(timeout <= 0)  
  72.     {  
  73.         timeout = INFINITE;  
  74.     }  
  75.     ::WaitForSingleObject(m_handle, timeout);  
  76. }  
  77.   
  78. void CThread::Resume()  
  79. {  
  80.     if(NULL == m_handle || !m_bRun)  
  81.     {  
  82.         return;  
  83.     }  
  84.     ::ResumeThread(m_handle);  
  85. }  
  86.   
  87. void CThread::Suspend()  
  88. {  
  89.     if(NULL == m_handle || !m_bRun)  
  90.     {  
  91.         return;  
  92.     }  
  93.     ::SuspendThread(m_handle);  
  94. }  
  95.   
  96. bool CThread::Terminate(unsigned long ExitCode)  
  97. {  
  98.     if(NULL == m_handle || !m_bRun)  
  99.     {  
  100.         return true;  
  101.     }  
  102.     if(::TerminateThread(m_handle, ExitCode))  
  103.     {  
  104.         ::CloseHandle(m_handle);  
  105.         return true;  
  106.     }  
  107.     return false;  
  108. }  
  109.   
  110. unsigned int CThread::GetThreadID()  
  111. {  
  112.     return m_ThreadID;  
  113. }  
  114.   
  115. std::string CThread::GetThreadName()  
  116. {  
  117.     return m_ThreadName;  
  118. }  
  119.   
  120. void CThread::SetThreadName(std::string ThreadName)  
  121. {  
  122.     m_ThreadName = ThreadName;  
  123. }  
  124.   
  125. void CThread::SetThreadName(const char * ThreadName)  
  126. {  
  127.     if(NULL == ThreadName)  
  128.     {  
  129.         m_ThreadName = "";  
  130.     }  
  131.     else  
  132.     {  
  133.         m_ThreadName = ThreadName;  
  134.     }  
  135. }  
  136.   
  137. unsigned int CThread::StaticThreadFunc(void * arg)  
  138. {  
  139.     CThread * pThread = (CThread *)arg;  
  140.     pThread->Run();  
  141.     return 0;  
  142. }  

 

 

用法:

#include "Thread.h"
#include "ThreadPoolExecutor.h"

class R : public Runnable
{
public:
    ~R()
    {
        printf("~R/n");
    }
    void Run()
    {
        printf("Hello World/n");
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    R r;
    CThread * t = NULL;
    t = new CThread(&r);
    t->Start();
    t->Join();

    getchar();

}

 

from:http://blog.csdn.net/huyiyang2010/article/details/5801597

目录
相关文章
|
2月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
153 11
|
2月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
196 1
|
2月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
221 1
|
8月前
|
存储 算法 C++
Windows共享文件:探秘C++实现的B树索引算法奇境
在数字化时代,Windows共享文件的高效管理至关重要。B树算法以其自平衡多路搜索特性,在文件索引与存储优化中表现出色。本文探讨B树在Windows共享文件中的应用,通过C++实现具体代码,展示其构建文件索引、优化数据存储的能力,提升文件检索效率。B树通过减少磁盘I/O操作,确保查询高效,为企业和个人提供流畅的文件共享体验。
|
缓存 安全 C++
C++无锁队列:解锁多线程编程新境界
【10月更文挑战第27天】
884 7
|
安全 Java
Java多线程集合类
本文介绍了Java中线程安全的问题及解决方案。通过示例代码展示了使用`CopyOnWriteArrayList`、`CopyOnWriteArraySet`和`ConcurrentHashMap`来解决多线程环境下集合操作的线程安全问题。这些类通过不同的机制确保了线程安全,提高了并发性能。
214 1
|
消息中间件 存储 安全
|
12月前
|
Java
【JavaEE】——多线程常用类
Callable的call方法,FutureTask类,ReentrantLock可重入锁和对比,Semaphore信号量(PV操作)CountDownLatch锁存器,
|
12月前
|
Java 程序员 调度
【JavaEE】线程创建和终止,Thread类方法,变量捕获(7000字长文)
创建线程的五种方式,Thread常见方法(守护进程.setDaemon() ,isAlive),start和run方法的区别,如何提前终止一个线程,标志位,isinterrupted,变量捕获
|
12月前
|
安全 Java API
【JavaEE】多线程编程引入——认识Thread类
Thread类,Thread中的run方法,在编程中怎么调度多线程