C++ 线程类的一个实现
.h
#ifndef CTHREAD_H_
#define CTHREAD_H_
#include "plat.h" class CThread
{
public:
enum
{
enmThreadTerminateTimeout = ,
};
CThread();
virtual ~CThread();
int32_t Start();
virtual int32_t Terminate();
virtual void Execute();
protected:
#ifdef OS_WIN32
uint32_t m_nTimeOut;
HANDLE m_hThread;
#else
pthread_t m_thread;
pthread_mutex_t m_stMutex;
#endif
bool m_bTerminated;
}; #endif
.cpp
#include "thread.h"
#include "common_api.h"
#ifdef OS_WIN32
void ThreadProc(void *pParam)
{
if (NULL == pParam)
{
return;
}
CThread *p = (CThread*)pParam;
p->Execute();
}
#else
void* ThreadProc(void* pParam)
{
if (NULL == pParam)
{
return NULL;
}
CThread *p = (CThread*)pParam;
p->Execute();
return NULL;
}
#endif CThread::CThread()
{
#ifdef OS_WIN32
m_nTimeOut = enmThreadTerminateTimeout;
m_hThread = INVALID_HANDLE_VALUE;
#else
m_thread = ;
#endif
m_bTerminated = false;
} CThread::~CThread()
{
Terminate();
} int32_t CThread::Start()
{
m_bTerminated = false;
#ifdef OS_WIN32
m_hThread = (HANDLE)_beginthread(ThreadProc, , this);
#else
pthread_create(&m_thread, NULL, ThreadProc, this);
#endif
return ;
} int32_t CThread::Terminate()
{
if (m_bTerminated == true)
{
return ;
}
m_bTerminated = true;
#ifdef OS_WIN32
if (m_hThread != INVALID_HANDLE_VALUE)
{
// wait for (m_nTimeOut) milliseconds or Excute() return
WaitForSingleObject(m_hThread, m_nTimeOut);
}
#else
if (m_thread != )
{
// wait Excute() return
pthread_join(m_thread, NULL);
}
#endif
return ;
} void CThread::Execute()
{
uint32_t n = ;
while (!m_bTerminated)
{
if ((++n) % == )
{
printf("%d\n", n);
}
Delay();
}
#ifdef WIN32
_endthread();
#else
pthread_exit(NULL);
#endif
}
C++ 线程类的一个实现的更多相关文章
- 【C/C++开发】C++实现简单的线程类
C++封装一个简单的线程类 多线程编程简介: 大家在编程时,经常需要在程序中启动一个或多个线程来处理任务,而如果每次都是去调用系统创建线程的API函数来创建,代码量虽不多,但线程的创建和业务逻辑代码就 ...
- 【Java】学习路径49-练习:使用两个不同的线程类实现买票系统
练习:使用两个不同的线程类实现买票系统 请创建两个不同的线程类.一个测试类以及一个票的管理类. 其中票的管理类用于储存票的数量.两个线程类看作不同的买票方式. 步骤: 1.创建所需的类 App售票线程 ...
- 转:一个跨WINDOWS LINUX平台的线程类
来源:http://blog.csdn.net/dengxu11/article/details/7232681 继Windows下实现一个CThread封装类之后,这里我再实现一个跨WINDOWS ...
- 从零开始构建一个Reactor模式的网络库(二)线程类Thread
线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...
- 转:学习笔记: Delphi之线程类TThread
学习笔记: Delphi之线程类TThread - 5207 - 博客园http://www.cnblogs.com/5207/p/4426074.html 新的公司接手的第一份工作就是一个多线程计算 ...
- 学习笔记: Delphi之线程类TThread
新的公司接手的第一份工作就是一个多线程计算的小系统.也幸亏最近对线程有了一些学习,这次一接手就起到了作用.但是在实际的开发过程中还是发现了许多的问题,比如挂起与终止的概念都没有弄明白,导致浪费许多的时 ...
- java: Thread 和 runnable线程类
java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...
- Delphi中线程类TThread实现多线程编程2---事件、临界区、Synchronize、WaitFor……
接着上文介绍TThread. 现在开始说明 Synchronize和WaitFor 但是在介绍这两个函数之前,需要先介绍另外两个线程同步技术:事件和临界区 事件(Event) 事件(Event)与De ...
- Delphi中线程类TThread实现多线程编程1---构造、析构……
参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...
随机推荐
- Xamarin.iOS项目提示error MSB3174:”TargetFrameworkVersion”的值无效
Xamarin.iOS项目提示error MSB3174:”TargetFrameworkVersion”的值无效 错误信息:MSBulid\14.0\bin\Microsoft.Common.Cur ...
- [LintCode] Word Break
Given a string s and a dictionary of words dict, determine if s can be break into a space-separated ...
- WCF 采用net.tcp协议实践
概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本 ...
- MathType 插入定义的chapter and section break后无法隐藏
每一章标题后面插入一个“Next Section Break”,这样定稿后各章文件组合为总文件后,方程编号会自动递增,如果已经插入了默认的“Equation Chapter 1 Section 1”, ...
- topcoder SRM 593 DIV2 RaiseThisBarn
#include <vector> #include <string> #include <list> #include <map> #include ...
- JS模态窗口返回值兼容问题解决方案
因系统要兼容原IE已使用的关闭方法,经调试测得,需对window.dialogArguments进行再较验,不然易出问题. function OKEnd(vals) { if (vals == nul ...
- [转] - Linux网络编程 -- 网络知识介绍
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- 关于 swift 的图片多选问题
http://stackoverflow.com/questions/20756899/how-to-select-multiple-images-from-uiimagepickercontroll ...
- 【iCore2 双核心板视频教程一】iM_LAN 100M 以太网模块UDP例程(包含视频教程)
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- Java判断一个时间是否在另一个时间段内
需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @para ...