一个Windows C++的线程类实现
Thread.h
- #ifndef __THREAD_H__
- #define __THREAD_H__
- #include <string>
- #include <windows.h>
- #include <process.h>
- class Runnable
- {
- public:
- virtual ~Runnable() {};
- virtual void Run() = 0;
- };
- class CThread : public Runnable
- {
- private:
- explicit CThread(const CThread & rhs);
- public:
- CThread();
- CThread(Runnable * pRunnable);
- CThread(const char * ThreadName, Runnable * pRunnable = NULL);
- CThread(std::string ThreadName, Runnable * pRunnable = NULL);
- ~CThread(void);
- /**
- 开始运行线程
- @arg bSuspend 开始运行时是否挂起
- **/
- bool Start(bool bSuspend = false);
- /**
- 运行的线程函数,可以使用派生类重写此函数
- **/
- virtual void Run();
- /**
- 当前执行此函数线程等待线程结束
- @arg timeout 等待超时时间,如果为负数,等待无限时长
- **/
- void Join(int timeout = -1);
- /**
- 恢复挂起的线程
- **/
- void Resume();
- /**
- 挂起线程
- **/
- void Suspend();
- /**
- 终止线程的执行
- **/
- bool Terminate(unsigned long ExitCode);
- unsigned int GetThreadID();
- std::string GetThreadName();
- void SetThreadName(std::string ThreadName);
- void SetThreadName(const char * ThreadName);
- private:
- static unsigned int WINAPI StaticThreadFunc(void * arg);
- private:
- HANDLE m_handle;
- Runnable * const m_pRunnable;
- unsigned int m_ThreadID;
- std::string m_ThreadName;
- volatile bool m_bRun;
- };
- #endif
Thread.cpp
- #include "Thread.h"
- CThread::CThread(void) :
- m_pRunnable(NULL),
- m_bRun(false)
- {
- }
- CThread::~CThread(void)
- {
- }
- CThread::CThread(Runnable * pRunnable) :
- m_ThreadName(""),
- m_pRunnable(pRunnable),
- m_bRun(false)
- {
- }
- CThread::CThread(const char * ThreadName, Runnable * pRunnable) :
- m_ThreadName(ThreadName),
- m_pRunnable(pRunnable),
- m_bRun(false)
- {
- }
- CThread::CThread(std::string ThreadName, Runnable * pRunnable) :
- m_ThreadName(ThreadName),
- m_pRunnable(pRunnable),
- m_bRun(false)
- {
- }
- bool CThread::Start(bool bSuspend)
- {
- if(m_bRun)
- {
- return true;
- }
- 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::Run()
- {
- if(!m_bRun)
- {
- return;
- }
- if(NULL != m_pRunnable)
- {
- m_pRunnable->Run();
- }
- m_bRun = false;
- }
- void CThread::Join(int timeout)
- {
- if(NULL == m_handle || !m_bRun)
- {
- return;
- }
- if(timeout <= 0)
- {
- timeout = INFINITE;
- }
- ::WaitForSingleObject(m_handle, timeout);
- }
- void CThread::Resume()
- {
- if(NULL == m_handle || !m_bRun)
- {
- return;
- }
- ::ResumeThread(m_handle);
- }
- void CThread::Suspend()
- {
- if(NULL == m_handle || !m_bRun)
- {
- return;
- }
- ::SuspendThread(m_handle);
- }
- bool CThread::Terminate(unsigned long ExitCode)
- {
- if(NULL == m_handle || !m_bRun)
- {
- return true;
- }
- if(::TerminateThread(m_handle, ExitCode))
- {
- ::CloseHandle(m_handle);
- 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;
- }
- void CThread::SetThreadName(const char * ThreadName)
- {
- if(NULL == ThreadName)
- {
- m_ThreadName = "";
- }
- else
- {
- m_ThreadName = ThreadName;
- }
- }
- unsigned int CThread::StaticThreadFunc(void * arg)
- {
- CThread * pThread = (CThread *)arg;
- pThread->Run();
- return 0;
- }
用法:
#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++的线程类实现的更多相关文章
- 一个Windows C++的线程类实现(封装API,形成一个类,但不完善。其实可以学习一下Delphi的TThread的写法)
Thread.h #ifndef __THREAD_H__ #define __THREAD_H__ #include <string> #include <windows.h& ...
- 一个Windows C++的线程池的实现
此线程池所依赖的线程类,请参看<一个Windows C++的线程类实现>: http://blog.csdn.net/huyiyang2010/archive/2010/08/10/580 ...
- Delphi中线程类TThread实现多线程编程2---事件、临界区、Synchronize、WaitFor……
接着上文介绍TThread. 现在开始说明 Synchronize和WaitFor 但是在介绍这两个函数之前,需要先介绍另外两个线程同步技术:事件和临界区 事件(Event) 事件(Event)与De ...
- Java带参数的线程类ParameterizedThread——即如何给Thread传递参数
在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...
- Delphi线程类 DIY(把类指针作为参数传进去,就可以执行类里面的方法啦)
Delphi 封装了一个很强大的线程类 TThread, 我们也自己动手制作一个简单的线程类 首先Type一个类 type TwwThread = class constructor Create; ...
- 转:一个跨WINDOWS LINUX平台的线程类
来源:http://blog.csdn.net/dengxu11/article/details/7232681 继Windows下实现一个CThread封装类之后,这里我再实现一个跨WINDOWS ...
- 一个Windows下线程池的实现(C++)
前言 本文配套代码:https://github.com/TTGuoying/ThreadPool 先看看几个概念: 线程:进程中负责执行的执行单元.一个进程中至少有一个线程. 多线程:一个进程中有多 ...
- C# 模拟一个处理消息队列的线程类 Message Queue
// 模拟一个处理消息队列的类 class MessageHandler { // 消息队列 private Queue<string> messageQue = new Queue< ...
- 从零开始构建一个Reactor模式的网络库(二)线程类Thread
线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...
随机推荐
- 如果有需要确解MD5的,可以尝试这个网站。
http://www.hashkiller.co.uk/md5-decrypter.aspx
- 自学HTML5第一天(认识HTML5的全局属性)
contextmenu 属性 规定 <div> 元素的上下文菜单.上下文菜单会在用户右键点击元素时出现.列子: <div contextmenu="mymenu" ...
- Datagridview控件实现分页功能
可以进行sql语句进行设置: 1.先新建一个窗体,一个DataGridView控件.两个label控件.两个Button控件 2.代码如下: using System; using Sy ...
- Google机器学习教程心得(一)
Hello world Google Machine Learning Recipes 1 官方中文博客 http://chinagdg.org/2016/03/machine-learning-re ...
- JSONObject和JSONArray
点击下载json工具 点击下载支持jar包 1.从Object到String 要先用Object对象构造一个JSONObject或者JSONArray对象,然后调用它的toString()方法即可 ( ...
- oracle 和informix 的基础区别
1:查看表空间 select b.file_name 物理文件名, b.tablespace_name 表空间, b.bytes/1024/1024 大小M, (b.bytes-sum(nvl(a.b ...
- rsyslog 日志服务器端配置
$EscapeControlCharactersOnReceive off #关闭rsyslog默认转译ASCII<32的所有怪异字符,包括换行符等 $template tocFormat,&q ...
- Jupyter Notebook通过latex输出pdf
主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{artic ...
- OpenGL红宝书学习笔记(1)
OpenGL对场景中的图像进行渲染时所执行的主要操作: 1.根据几何图元创建形状,从而建立物体的数学描述,(OpenGL把点,直线,多边形和位图作为基本的图元) 2.在三维空间中排列物体,并选择观察复 ...
- aix rksh 执行CLI命令行限制
su padmin -C "lsdev -dev hdisk9 -attr| grep unique_id"rksh: lsdev: 0403-006 Execute permis ...