面向对象与基于对象 学习记录 thread举例
/********************************************************************/
* @file
* @author def< qq group: 324164944 >
* @blog http://www.cnblogs.com/itdef/
* @brief
/********************************************************************/
/*******************************************************************************
* @file
* @author def< qq group: 324164944 >
* @blog http://www.cnblogs.com/itdef/
* @brief
/*******************************************************************************/ #include "stdafx.h"
#include <iostream>
#include <windows.h> using namespace std; class CThread{
public:
CThread();
virtual ~CThread();
bool Start();
void Join();
static DWORD WINAPI ThreadProc( LPVOID lpParameter);
virtual void Run() = 0;
private:
HANDLE hThread_;
DWORD dwThreadId_;
}; CThread::CThread():
hThread_(NULL),dwThreadId_(0)
{
cout << "Thread ..." << endl;
} CThread::~CThread()
{
if(hThread_ != NULL)
CloseHandle(hThread_);
cout << "~Thread ..." << endl;
} bool CThread::Start()
{
bool bRet = false;
hThread_ = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId_); // returns the thread identifier if(hThread_)
{
bRet = true;
}
return bRet;
} void CThread::Join()
{
WaitForSingleObject(hThread_,3000);
} DWORD CThread::ThreadProc( LPVOID lpParameter)
{
CThread* thread = static_cast<CThread*>(lpParameter);
thread->Run();
return NULL;
} class CMyThread:public CThread
{
public:
void Run(){ cout << "my thread..." << endl;}
}; int _tmain(int argc, _TCHAR* argv[])
{
CMyThread thread;
thread.Start();
thread.Join();
return 0;
}
基类是最基本的几个元素 线程ID 创建进程的函数start 运行指定的线程函数run 以及等待函数join()
使用的时候直接继承 在run函数中执行自己想执行的线程处理即可。
基于对象则未使用继承等特性,使用bind function这对利器 来实现回调
#include <windows.h>
#include <iostream>
#include <boost/function.hpp> class CThread
{
public:
typedef boost::function<void ()> ThreadFunc;
explicit CThread(const ThreadFunc& func);
~CThread(); void Start();
void Join();
private:
static DWORD WINAPI ThreadProc(LPVOID arg);
void Run();
ThreadFunc func_;
HANDLE hThread_; }; void CThread::Start()
{
hThread_ = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
this, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
} CThread::CThread(const ThreadFunc& func):
hThread_(NULL),func_(func)
{
std::cout << "CThread()..." << std::endl;
} void CThread::Join()
{
WaitForSingleObject(hThread_,3000);
} CThread::~CThread()
{
if(hThread_)
CloseHandle(hThread_);
std::cout << "~CThread()..." << std::endl;
} void CThread::Run()
{
func_();
} DWORD CThread::ThreadProc(LPVOID arg)
{
CThread* thread = static_cast<CThread*>(arg);
thread->Run();
return NULL;
} //====================================== void ThreadFunc()
{
std::cout << "Enter thread function ...." << std::endl;
} int _tmain(int argc, _TCHAR* argv[])
{
CThread thread(ThreadFunc);
thread.Start();
thread.Join();
return 0;
}
面向对象与基于对象 学习记录 thread举例的更多相关文章
- Git 教程 -- 基于自己学习记录
Git 教程 -- 基于自己学习记录 1. 引言 由于学校布置了一项熟悉 git 和 svn 操作的实验,所以自己重新温习了下 git,记录过程在这. 2. 注册登录 GitHub. 3. 选择一个仓 ...
- JS是面向过程、面向对象还是基于对象?面向对象的代码体现
一.问题 javascript是面向对象的,还是面向过程的?基于对象是什么意思? 对象: 指的是对某一类事物进行抽象,抽象出这一类事物共同的特征以及行为(也就是属性和方法),那些拥有这一共同属性和方法 ...
- SVN教程 -- 基于自己学习记录
SVN教程 -- 基于自己学习记录 1. 概述 a. 什么是SVN? Apache Subversion 通常被缩写成 SVN,是一个开放源代码的版本控制系统.相较于 git ,svn 是集中式版本控 ...
- 重学前端--js是面向对象还是基于对象?
重学前端-面向对象 跟着winter老师一起,重新认识前端的知识框架 js面向对象或基于对象编程 以前感觉这两个在本质上没有什么区别,面向对象和基于对象都是对一个抽象的对象拥有一系列的行为和状态,本质 ...
- 流畅的python第九章符合Python风格的对象学习记录
对象表示形式 每门面向对象的语言至少都有一种获取对象的字符串表示形式的标准方式.Python提供了两种方式 repr()便于开发者理解的方式返回对象的字符串表示形式 str()便于用户理解的方式返回对 ...
- jsp内置对象学习记录
1.session,是一个会话保留在服务器端的对象(默认保留时间为30分钟),所以我们可以在session里面放用户信息以便后续的访问便利(缺点:cookie劫持,导致用户数据泄露).案例:(1)同个 ...
- C++11新特性,bind,基于对象
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- {django模型层(二)多表操作}一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询、分组查询、F查询和Q查询
Django基础五之django模型层(二)多表操作 本节目录 一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询.分组查询.F查询和Q查询 六 xxx 七 ...
- Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...
随机推荐
- hibernate vs ibatis
主要通过 灵活性,性能,开发速度 三个角度来看 1.ibatis非常简单易学,hibernate相对较复杂,门槛较高. 2.二者都是比较优秀的开源产品 3.当系统属于二次开发,无法对数据库结构做到 ...
- svn使用---在CentOS 7上搭建SVN服务器 及windows搭建svn步骤
svn搭建方法: https://blog.csdn.net/helijie92902/article/details/51935122?foxhandler=RssReadRenderProcess ...
- leetcode933
public class RecentCounter { Queue<int> Q; public RecentCounter() { Q = new Queue<int>() ...
- leetcode91
class Solution { public int numDecodings(String s) { if(s.length()==0){ return 0; } int[] dp = new i ...
- 使用java代码执行linux命令
前提: java代码是在windows下面写的,要打包放到linux下面运行,并且执行某个脚本. java代码: try { // 起作用的代码其实就下面这一行, 参数是linux中要执行的代码 Ru ...
- 转载:mysql数据同步redis
from: http://www.cnblogs.com/zhxilin/archive/2016/09/30/5923671.html 在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终 ...
- git创建仓库,并提交代码(第一次创建并提交)(转)
一直想学GIT,一直不曾学会.主要是GUI界面的很少,命令行大多记不住.今天尝试提交代码,按GIT上给的方法,没料到既然提交成功了. 于是把它记下来,方便以后学习. 代码是学习用的,没多大意义: 下图 ...
- from __future__ import division
导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精 ...
- Unity C# 调用 C++ DLL 并在 DLL 中调用 C# 的回调函数
Unity C# 调用 C++ DLL 并在 DLL 中调用 C# 的回调函数~~~ 呵呵... 看着有点晕.. 再解释一下就是 在Unity中 使用 C# 调用 C++ 写的 DLL, 但是在 ...
- 第一个struct2程序(2)
第三步 需要使用ActionForm了.在Struts1.x中,必须要单独建立一个ActionForm类(或是定义一个动作Form),而在Struts2中ActionForm和Action已经二合一了 ...