Thread.h

  1. #ifndef __THREAD_H__
  2. #define __THREAD_H__
  3. #include <string>
  4. #include   <windows.h>
  5. #include   <process.h>
  6. class Runnable
  7. {
  8. public:
  9. virtual ~Runnable() {};
  10. virtual void Run() = 0;
  11. };
  12. class CThread : public Runnable
  13. {
  14. private:
  15. explicit CThread(const CThread & rhs);
  16. public:
  17. CThread();
  18. CThread(Runnable * pRunnable);
  19. CThread(const char * ThreadName, Runnable * pRunnable = NULL);
  20. CThread(std::string ThreadName, Runnable * pRunnable = NULL);
  21. ~CThread(void);
  22. /**
  23. 开始运行线程
  24. @arg bSuspend 开始运行时是否挂起
  25. **/
  26. bool Start(bool bSuspend = false);
  27. /**
  28. 运行的线程函数,可以使用派生类重写此函数
  29. **/
  30. virtual void Run();
  31. /**
  32. 当前执行此函数线程等待线程结束
  33. @arg timeout 等待超时时间,如果为负数,等待无限时长
  34. **/
  35. void Join(int timeout = -1);
  36. /**
  37. 恢复挂起的线程
  38. **/
  39. void Resume();
  40. /**
  41. 挂起线程
  42. **/
  43. void Suspend();
  44. /**
  45. 终止线程的执行
  46. **/
  47. bool Terminate(unsigned long ExitCode);
  48. unsigned int GetThreadID();
  49. std::string GetThreadName();
  50. void SetThreadName(std::string ThreadName);
  51. void SetThreadName(const char * ThreadName);
  52. private:
  53. static unsigned int WINAPI StaticThreadFunc(void * arg);
  54. private:
  55. HANDLE m_handle;
  56. Runnable * const m_pRunnable;
  57. unsigned int m_ThreadID;
  58. std::string m_ThreadName;
  59. volatile bool m_bRun;
  60. };
  61. #endif

Thread.cpp

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

用法:

#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

一个Windows C++的线程类实现的更多相关文章

  1. 一个Windows C++的线程类实现(封装API,形成一个类,但不完善。其实可以学习一下Delphi的TThread的写法)

    Thread.h #ifndef __THREAD_H__ #define __THREAD_H__ #include <string> #include   <windows.h& ...

  2. 一个Windows C++的线程池的实现

    此线程池所依赖的线程类,请参看<一个Windows C++的线程类实现>: http://blog.csdn.net/huyiyang2010/archive/2010/08/10/580 ...

  3. Delphi中线程类TThread实现多线程编程2---事件、临界区、Synchronize、WaitFor……

    接着上文介绍TThread. 现在开始说明 Synchronize和WaitFor 但是在介绍这两个函数之前,需要先介绍另外两个线程同步技术:事件和临界区 事件(Event) 事件(Event)与De ...

  4. Java带参数的线程类ParameterizedThread——即如何给Thread传递参数

    在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...

  5. Delphi线程类 DIY(把类指针作为参数传进去,就可以执行类里面的方法啦)

    Delphi 封装了一个很强大的线程类 TThread, 我们也自己动手制作一个简单的线程类 首先Type一个类 type TwwThread = class constructor Create;  ...

  6. 转:一个跨WINDOWS LINUX平台的线程类

     来源:http://blog.csdn.net/dengxu11/article/details/7232681 继Windows下实现一个CThread封装类之后,这里我再实现一个跨WINDOWS ...

  7. 一个Windows下线程池的实现(C++)

    前言 本文配套代码:https://github.com/TTGuoying/ThreadPool 先看看几个概念: 线程:进程中负责执行的执行单元.一个进程中至少有一个线程. 多线程:一个进程中有多 ...

  8. C# 模拟一个处理消息队列的线程类 Message Queue

    // 模拟一个处理消息队列的类 class MessageHandler { // 消息队列 private Queue<string> messageQue = new Queue< ...

  9. 从零开始构建一个Reactor模式的网络库(二)线程类Thread

    线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...

随机推荐

  1. 1011. A+B和C

    /* * Main.c * 1011. A+B和C * Created on: 2014年8月30日 * Author: Boomkeeper *********测试通过******* */ #inc ...

  2. Java 中使用Jackson反序列化

    Build.gradle: compile group: 'org.codehaus.jackson', name: 'jackson-mapper-lgpl', version: '1.9.13' ...

  3. SQL Server 移动系统数据库位置(非master)

    以移动tempdb为例子: -------------------------------------------------------------------------------------- ...

  4. 给ecshop后台增加管理功能页面

    给ecshop后台增加管理功能页面 比如我们增加一个统计报表叫做 物流费用统计报表 放在后台“报表统计”栏目中 具体操作步骤: 第一步,我们要添加一个菜单到后台,然后设置语言项,最后设置权限,这样,后 ...

  5. 每天一道题:LeetCode

    本人是研二程旭猿一枚,还有半年多就要找工作了,想想上一年度面试阿里的算法工程师挂了,心有不甘啊,主要还是准备不足,对一些常见的算法问题没有去组织准备,为了明年找一份好的实习,就从现在开始,好好准备吧, ...

  6. 基于Platinum库的DMS实现(android)

    接上篇博文:基于Platinum库的DMR实现(android) 文章讲述了如何使用Platinum库实现DMR 今天同样使用该库,来讲解一下DMS的实现 关于该库如何编译,请参考这篇博文:NDK下 ...

  7. Spring、Spring自动扫描和管理Bean

    Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component.@Service.@Controller.@Repository注解的类,并把这些类纳入到spring容 ...

  8. setInterval和setTimeout的使用区别

    setTimeout和setInterval的使用 这两个方法都可以用来实现在一个固定时间段之后去执行JavaScript.不过两者各有各的应用场景. 方 法 实际上,setTimeout和setIn ...

  9. c++适配器模式

    你想使用一个已经存在的类,而它的接口不符合你的需求. 创建一个类需要和其他类协同完成任务,需要一个适配器将其他类的方法都转接到适配器当中 什么是适配器模式:有一个目标客户类想适用已经存在类的接口,但是 ...

  10. 多线程编程 - GCD(转)

    原文:http://blog.csdn.net/q199109106q/article/details/8566300 一.简介 在iOS所有实现多线程的方案中,GCD应该是最有魅力的,因为GCD本身 ...