/********************************************************************/
* @file
* @author def< qq group: 324164944 >
* @blog http://www.cnblogs.com/itdef/
* @brief
/********************************************************************/

  1. /*******************************************************************************
  2. * @file
  3. * @author def< qq group: 324164944 >
  4. * @blog http://www.cnblogs.com/itdef/
  5. * @brief
  6. /*******************************************************************************/
  7.  
  8. #include "stdafx.h"
  9. #include <iostream>
  10. #include <windows.h>
  11.  
  12. using namespace std;
  13.  
  14. class CThread{
  15. public:
  16. CThread();
  17. virtual ~CThread();
  18. bool Start();
  19. void Join();
  20. static DWORD WINAPI ThreadProc( LPVOID lpParameter);
  21. virtual void Run() = 0;
  22. private:
  23. HANDLE hThread_;
  24. DWORD dwThreadId_;
  25. };
  26.  
  27. CThread::CThread():
  28. hThread_(NULL),dwThreadId_(0)
  29. {
  30. cout << "Thread ..." << endl;
  31. }
  32.  
  33. CThread::~CThread()
  34. {
  35. if(hThread_ != NULL)
  36. CloseHandle(hThread_);
  37. cout << "~Thread ..." << endl;
  38. }
  39.  
  40. bool CThread::Start()
  41. {
  42. bool bRet = false;
  43. hThread_ = CreateThread(
  44. NULL, // default security attributes
  45. 0, // use default stack size
  46. ThreadProc, // thread function
  47. this, // argument to thread function
  48. 0, // use default creation flags
  49. &dwThreadId_); // returns the thread identifier
  50.  
  51. if(hThread_)
  52. {
  53. bRet = true;
  54. }
  55. return bRet;
  56. }
  57.  
  58. void CThread::Join()
  59. {
  60. WaitForSingleObject(hThread_,3000);
  61. }
  62.  
  63. DWORD CThread::ThreadProc( LPVOID lpParameter)
  64. {
  65. CThread* thread = static_cast<CThread*>(lpParameter);
  66. thread->Run();
  67. return NULL;
  68. }
  69.  
  70. class CMyThread:public CThread
  71. {
  72. public:
  73. void Run(){ cout << "my thread..." << endl;}
  74. };
  75.  
  76. int _tmain(int argc, _TCHAR* argv[])
  77. {
  78.   CMyThread thread;
  79.   thread.Start();  
  80.   thread.Join();
  81.   return 0;
  82. }

  

基类是最基本的几个元素 线程ID 创建进程的函数start 运行指定的线程函数run  以及等待函数join()

使用的时候直接继承 在run函数中执行自己想执行的线程处理即可。

基于对象则未使用继承等特性,使用bind function这对利器 来实现回调

  1. #include <windows.h>
  2. #include <iostream>
  3. #include <boost/function.hpp>
  4.  
  5. class CThread
  6. {
  7. public:
  8. typedef boost::function<void ()> ThreadFunc;
  9. explicit CThread(const ThreadFunc& func);
  10. ~CThread();
  11.  
  12. void Start();
  13. void Join();
  14. private:
  15. static DWORD WINAPI ThreadProc(LPVOID arg);
  16. void Run();
  17. ThreadFunc func_;
  18. HANDLE hThread_;
  19.  
  20. };
  21.  
  22. void CThread::Start()
  23. {
  24. hThread_ = CreateThread(
  25. NULL, // default security attributes
  26. 0, // use default stack size
  27. ThreadProc, // thread function
  28. this, // argument to thread function
  29. 0, // use default creation flags
  30. NULL); // returns the thread identifier
  31. }
  32.  
  33. CThread::CThread(const ThreadFunc& func):
  34. hThread_(NULL),func_(func)
  35. {
  36. std::cout << "CThread()..." << std::endl;
  37. }
  38.  
  39. void CThread::Join()
  40. {
  41. WaitForSingleObject(hThread_,3000);
  42. }
  43.  
  44. CThread::~CThread()
  45. {
  46. if(hThread_)
  47. CloseHandle(hThread_);
  48. std::cout << "~CThread()..." << std::endl;
  49. }
  50.  
  51. void CThread::Run()
  52. {
  53. func_();
  54. }
  55.  
  56. DWORD CThread::ThreadProc(LPVOID arg)
  57. {
  58. CThread* thread = static_cast<CThread*>(arg);
  59. thread->Run();
  60. return NULL;
  61. }
  62.  
  63. //======================================
  64.  
  65. void ThreadFunc()
  66. {
  67. std::cout << "Enter thread function ...." << std::endl;
  68. }
  69.  
  70. int _tmain(int argc, _TCHAR* argv[])
  71. {
  72. CThread thread(ThreadFunc);
  73. thread.Start();
  74. thread.Join();
  75. return 0;
  76. }

  

面向对象与基于对象 学习记录 thread举例的更多相关文章

  1. Git 教程 -- 基于自己学习记录

    Git 教程 -- 基于自己学习记录 1. 引言 由于学校布置了一项熟悉 git 和 svn 操作的实验,所以自己重新温习了下 git,记录过程在这. 2. 注册登录 GitHub. 3. 选择一个仓 ...

  2. JS是面向过程、面向对象还是基于对象?面向对象的代码体现

    一.问题 javascript是面向对象的,还是面向过程的?基于对象是什么意思? 对象: 指的是对某一类事物进行抽象,抽象出这一类事物共同的特征以及行为(也就是属性和方法),那些拥有这一共同属性和方法 ...

  3. SVN教程 -- 基于自己学习记录

    SVN教程 -- 基于自己学习记录 1. 概述 a. 什么是SVN? Apache Subversion 通常被缩写成 SVN,是一个开放源代码的版本控制系统.相较于 git ,svn 是集中式版本控 ...

  4. 重学前端--js是面向对象还是基于对象?

    重学前端-面向对象 跟着winter老师一起,重新认识前端的知识框架 js面向对象或基于对象编程 以前感觉这两个在本质上没有什么区别,面向对象和基于对象都是对一个抽象的对象拥有一系列的行为和状态,本质 ...

  5. 流畅的python第九章符合Python风格的对象学习记录

    对象表示形式 每门面向对象的语言至少都有一种获取对象的字符串表示形式的标准方式.Python提供了两种方式 repr()便于开发者理解的方式返回对象的字符串表示形式 str()便于用户理解的方式返回对 ...

  6. jsp内置对象学习记录

    1.session,是一个会话保留在服务器端的对象(默认保留时间为30分钟),所以我们可以在session里面放用户信息以便后续的访问便利(缺点:cookie劫持,导致用户数据泄露).案例:(1)同个 ...

  7. C++11新特性,bind,基于对象

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  8. {django模型层(二)多表操作}一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询、分组查询、F查询和Q查询

    Django基础五之django模型层(二)多表操作 本节目录 一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询.分组查询.F查询和Q查询 六 xxx 七 ...

  9. Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...

随机推荐

  1. VisualSVN:强制必须填写日志信息

    上回将到怎么修改已提交的版本日志信息,而开发项目过程中团队中总是有人忘记添加日志信息注释直接提交,这样会后期维护带来不便. 现在先演示一下效果 当直接提交一个空白日志信息时 有填写日志信息时 那怎么实 ...

  2. VBA 禁止保存

    禁止保存 在workbook事件中 Private Sub Workbook_BeforeClose(Cancel As Boolean)    Me.Saved = TrueEnd Sub Priv ...

  3. 进程池(Pool)

    进程池用于进程维护, 当使用时,将会去进程池取数据 from multiprocessing import Pool, Processimport os, time def f(i): time.sl ...

  4. CentOS 7 Tomcat安装

    官网: http://tomcat.apache.org/download-80.cgi 下 1.载zip包 >wget http://mirrors.hust.edu.cn/apache/to ...

  5. hibernate 异常

    1.异常:org.hibernate.AnnotationException: No identifier specified for entity异常. entity类是必须要主键的,否则就会报出这 ...

  6. PYTHON-进阶-装饰器小结,转载

    本文转载自:http://www.wklken.me/posts/2012/10/27/python-base-decorator.html 基本概念 具体概念自己google 装饰器是一个很著名的设 ...

  7. JVM gc介绍

    Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ...

  8. ant使用备忘

    ant是一个脚本构建工具,可能就是持续集成里面所需要的构建工具. 如果使用eclipse,里面会自带有ant工具,不需要再安装了,创建一个build.xml(或者其他的名字都可以),使用ant来运行就 ...

  9. C++ 0x 使用 shared_ptr 自动释放, 防止内存泄漏

    最近在研究 cocos2d-x 3.0 ,它在创建类的对象时比如 Layer 时, 并不是直接使用 new , 而是使用一个宏方法  CREATE_FUNC(MyLayer);. 这个宏就是自动的创建 ...

  10. 02 Tensorflow的安装配置

    1 anaconda 64 位,win10 安装 清华大学镜像网络,下载地址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 选择下载文件 ...