.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++ 线程类的一个实现的更多相关文章

  1. 【C/C++开发】C++实现简单的线程类

    C++封装一个简单的线程类 多线程编程简介: 大家在编程时,经常需要在程序中启动一个或多个线程来处理任务,而如果每次都是去调用系统创建线程的API函数来创建,代码量虽不多,但线程的创建和业务逻辑代码就 ...

  2. 【Java】学习路径49-练习:使用两个不同的线程类实现买票系统

    练习:使用两个不同的线程类实现买票系统 请创建两个不同的线程类.一个测试类以及一个票的管理类. 其中票的管理类用于储存票的数量.两个线程类看作不同的买票方式. 步骤: 1.创建所需的类 App售票线程 ...

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

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

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

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

  5. 转:学习笔记: Delphi之线程类TThread

    学习笔记: Delphi之线程类TThread - 5207 - 博客园http://www.cnblogs.com/5207/p/4426074.html 新的公司接手的第一份工作就是一个多线程计算 ...

  6. 学习笔记: Delphi之线程类TThread

    新的公司接手的第一份工作就是一个多线程计算的小系统.也幸亏最近对线程有了一些学习,这次一接手就起到了作用.但是在实际的开发过程中还是发现了许多的问题,比如挂起与终止的概念都没有弄明白,导致浪费许多的时 ...

  7. java: Thread 和 runnable线程类

    java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...

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

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

  9. Delphi中线程类TThread实现多线程编程1---构造、析构……

    参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...

随机推荐

  1. ural 1269. Obscene Words Filter

    1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check mess ...

  2. 金子上的友情[XDU1011]

    Problem 1011 - 金子上的友情 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 336  Ac ...

  3. Codeforces Round #192 (Div. 2) B. Road Construction

    #include <iostream> #include <vector> using namespace std; int main(){ int n,m; cin > ...

  4. 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...

  5. 【wikioi】1904 最小路径覆盖问题(最大流+坑人的题+最小路径覆盖)

    http://wikioi.com/problem/1904/ 这题没看数据的话是一个大坑(我已报告官方修复了),答案只要求数量,不用打印路径...orz 最小路径覆盖=n-最大匹配,这个我在说二分图 ...

  6. 【POJ】1062 昂贵的聘礼(spfa)

    http://poj.org/problem?id=1062 此题一开始果断想到暴力.. 但是n<=100果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...

  7. 【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1270 这完全是一眼题啊,但是n^2的时间挺感人.(n^2一下的级别请大神们赐教,我还没学多少dp优化 ...

  8. HTML<marquee>标签

    <marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容.<marquee>标签的属性主要 ...

  9. 【转载】Erlang 中 link 和 monitor 的区别

    Link and Monitor differences 原文地址 Introduction link/1 and monitor/2 are 2 different ways of notifyin ...

  10. SqlParameter 基本用法

    因为通过SQL 语句的方式,有时候存在脚本注入的危险,所以在大多数情况下不建议用拼接SQL语句字符串方式,希望通过SqlParameter实现来实现对数据的操 作,针对SqlParameter的方式我 ...