大家知道,Linux的线程操作是使用pthread库。这个库都是c函数,如果你使用c++语言开发的话,线程处理部分不能封装起来是一个很麻烦的事情,从面向对象角度,系统结构等角度来看都很不舒服。我曾经找过是否有别人编写的c++的线程处理库,但是没有找到。于是我自己写了一个,希望能对大家有点用处。当然也可能存在问题,希望大家指出。我的信箱是 hezhenwei#hotmail.com
点这里下载
使用方法:
把thread-1.0-1.i386.rpm安装到你机器上。
创建一个class , public 继承 Thread 类。需要包含的头文件是 <thread.h>
重载 virtual void Routine(void) 方法,写入你的线程所要做的事情。
运行时,只要使用对象的Run方法。该方法立即返回。同时已经有一个线程在运行了。
下面讲讲主要的思路:
建立一个class 叫做 Thread。然后建立方法Routine和Run。
在同一个文件中建立一个线程。
在Run方法中创建该线程,同时把本身的对象指针 this 作为参数传递给线程
在线程中调用this指针的Routine方法。
以上就可以把一个线程封装在类Thread中了。
然后我把Routine方法作为虚函数,这样该类的子类就可以重载该方法以实现不同的线程操作。
主要代码如下
thread.h文件
#ifndef _THREAD_H_HEZHENWEI_ASDFASDF
#define _THREAD_H_HEZHENWEI_ASDFASDF
#include <pthread.h>
void* thd(void* pars);
class Thread
{
private:
pthread_t m_tht;
int m_nPid;
public:
void SetPid(const int nPid);
int GetPid(void);
void run(void);
virtual void Routine(void);
bool isRunning(void);
};
#endif
thread.cpp文件
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include "thread.h"
void* thd(void* pars)
{
Thread* p1;
p1=(Thread*)pars;
int nPid = getpid();
p1->SetPid(nPid);
p1->Routine();
return NULL;
}
void Thread::run(void)
{
pthread_create(&m_tht,NULL,&thd,(void*)this);
}
void Thread::Routine(void)
{
printf("thread is running\n");
}
void Thread::SetPid(int nPid)
{
m_nPid = nPid;
}
int Thread::GetPid(void)
{
return m_nPid;
}
bool Thread::isRunning(void)
{
int nRet;
nRet = kill(m_nPid,SIGCHLD);
if(0!=nRet)
{
return false;
}
else
{
return true;
}
}
点这里下载
使用方法:
把thread-1.0-1.i386.rpm安装到你机器上。
创建一个class , public 继承 Thread 类。需要包含的头文件是 <thread.h>
重载 virtual void Routine(void) 方法,写入你的线程所要做的事情。
运行时,只要使用对象的Run方法。该方法立即返回。同时已经有一个线程在运行了。
下面讲讲主要的思路:
建立一个class 叫做 Thread。然后建立方法Routine和Run。
在同一个文件中建立一个线程。
在Run方法中创建该线程,同时把本身的对象指针 this 作为参数传递给线程
在线程中调用this指针的Routine方法。
以上就可以把一个线程封装在类Thread中了。
然后我把Routine方法作为虚函数,这样该类的子类就可以重载该方法以实现不同的线程操作。
主要代码如下
thread.h文件
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
thread.cpp文件
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)