Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103127
  • 博文数量: 51
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-08 10:52
文章分类

全部博文(51)

文章存档

2016年(5)

2015年(3)

2014年(43)

我的朋友

分类: LINUX

2014-03-14 09:26:52

在原来的基础上,进行了加强,使之进一步接近 Java 中 Thread 和 Runnable 的用法。

下面是我写的基类,把代码保存在名为 Thread.h 的头文件中。
====================================传说中的分割线=================================
/*
* File: Thread.h
* Author: Null
* Blog:
* Created on 2008年7月30日, 上午10:13
*/

/*
* 在编译的时候记得加上参数:-lpthread
*
*/

#ifndef _THREAD_H
#define _THREAD_H


#include <pthread.h>
#include <unistd.h>

/*
* 线程运行实体类
*/
class Runnable
{
public:
//运行实体
virtual void run() = 0;
};

/*
* 线程类
*
*/
class Thread : public Runnable
{
private:
//线程初始化序号
static int threadInitNumber;
//当前线程初始化序号
int curThreadInitNumber;
//线程体
Runnable *target;
//当前线程的线程ID
pthread_t tid;
//线程的状态
int threadStatus;
//线程属性
pthread_attr_t attr;
//线程优先级
sched_param param;
//获取执行方法的指针
static void* run0(void* pVoid);
//内部执行方法
void* run1();
//获取一个线程序号
static int getNextThreadNum();

public:
//线程的状态-新建
static const int THREAD_STATUS_NEW = 0;
//线程的状态-正在运行
static const int THREAD_STATUS_RUNNING = 1;
//线程的状态-运行结束
static const int THREAD_STATUS_EXIT = -1;
//构造函数
Thread();
//构造函数
Thread(Runnable *iTarget);
//析构
~Thread();
//线程的运行实体
void run();
//开始执行线程
bool start();
//获取线程状态
int getState();
//等待线程直至退出
void join();
//等待线程退出或者超时
void join(unsigned long millisTime);
//比较两个线程时候相同,通过 curThreadInitNumber 判断
bool operator ==(const Thread *otherThread);
//获取This线程ID
pthread_t getThreadID();
//获取当前线程ID
static pthread_t getCurrentThreadID();
//当前线程是否和某个线程相等,通过 tid 判断
static bool isEquals(Thread *iTarget);
//设置线程的类型:绑定/非绑定
void setThreadScope(bool isSystem);
//获取线程的类型:绑定/非绑定
bool getThreadScope();
//设置线程的优先级,1-99,其中99为实时;意外的为普通
void setThreadPriority(int priority);
//获取线程的优先级
int getThreadPriority();
};

#endif /* _THREAD_H */

int Thread::threadInitNumber = 1;

int Thread::getNextThreadNum()
{
return threadInitNumber++;
}

void* Thread::run0(void* pVoid)
{
Thread* p = (Thread*) pVoid;
p->run1();
return p;
}

void* Thread::run1()
{

threadStatus = THREAD_STATUS_RUNNING;
tid = pthread_self();
run();
threadStatus = THREAD_STATUS_EXIT;
tid = 0;
pthread_exit(NULL);
}

void Thread::run()
{
if (target != NULL)
{
(*
target).run();
}
}

Thread::Thread()
{
tid = 0;
threadStatus = THREAD_STATUS_NEW;
curThreadInitNumber = getNextThreadNum();
pthread_attr_init(&attr);
}

Thread::Thread(Runnable *iTarget)
{
target = iTarget;
tid = 0;
threadStatus = THREAD_STATUS_NEW;
curThreadInitNumber = getNextThreadNum();
pthread_attr_init(&attr);
}

Thread::~Thread()
{
pthread_attr_destroy(&attr);
}

bool Thread::start()
{
return pthread_create(&tid, &attr, run0, this) == 0;
}

pthread_t Thread::getCurrentThreadID()
{
return pthread_self();
}

pthread_t Thread::getThreadID()
{
return tid;
}

int Thread::getState()
{
return threadStatus;
}

void Thread::join()
{
if (tid > 0)
{
pthread_join(tid, NULL);
}
}

void Thread::join(unsigned long millisTime)
{

if (tid == 0)
{
return;
}
if (millisTime == 0)
{
join();
}
else
{
unsigned long k = 0;
while (threadStatus != THREAD_STATUS_EXIT && k <= millisTime)
{
usleep(100);
k++;
}
}
}

bool Thread::operator ==(const Thread *otherThread)
{
if (otherThread == NULL)
{
return false;
}
if (curThreadInitNumber == (*otherThread).curThreadInitNumber)
{
return true;
}
return false;
}

bool Thread::isEquals(Thread *iTarget)
{
if (iTarget == NULL)
{
return false;
}
return pthread_self() == iTarget->tid;
}

void Thread::setThreadScope(bool isSystem)
{
if (isSystem)
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
}
else
{
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
}
}

bool Thread::getThreadScope()
{
int scopeType = 0;
pthread_attr_getscope(&attr, &scopeType);
return scopeType == PTHREAD_SCOPE_SYSTEM;
}

void Thread::setThreadPriority(int priority)
{
pthread_attr_getschedparam(&attr, &param);
param.__sched_priority = priority;
pthread_attr_setschedparam(&attr, &param);
}

int Thread::getThreadPriority()
{
pthread_attr_getschedparam(&attr, &param);
return param.__sched_priority;
}

====================================传说中的分割线=================================


具体看下面的示例代码。
呵呵,用法和 Java 的 Thread 很相似吧。
下面一共生成了3个线程,3个线程都共享 MultiThread::run()
在 run() 里面通过判断线程的实体来分离各个线程的工作。

====================================传说中的分割线=================================
/*
* File: newmain.cc
* Author: Null
* Blog:
* Created on 2008年7月30日, 上午12:49
*/


#include "Thread.h"
#include <iostream.h>

class MultiThread : public Thread
{
public:

Thread *th1;
Thread *th2;

void Test()
{
th1 = new Thread(this);
th1->setThreadPriority(99);
th2 = new Thread(this);
start();
th1->start();
th2->start();
th1->join();
th2->join();
}

void run()
{
//Thread->isEquals(th1)
if (Thread::isEquals(th1))
{
int number = 100;
for (int i = 0; i < 10; i++)
{
cout << "this is thread1 number is " << number++;
cout << " \tpid is " << getpid() << " tid is " << getCurrentThreadID() << " Priority:" << th1->getThreadPriority() << endl;
sleep(1);
}
}
else if (Thread::isEquals(th2))
{
int number = 200;
for (int i = 0; i < 10; i++)
{
cout << "this is thread2 number is " << number++;
cout << " \tpid is " << getpid() << " tid is " << getCurrentThreadID() << " Priority:" << th2->getThreadPriority() << endl;
sleep(1);
}
}
else if (Thread::isEquals(this))
{
int number = 300;
for (int i = 0; i < 10; i++)
{
cout << "this is thread0 number is " << number++;
cout << " \tpid is " << getpid() << " tid is " << getCurrentThreadID() << " Priority:" << this->getThreadPriority() << endl;
sleep(1);
}
}

}
};

/*
*
*/
int main(int argc, char** argv)
{
bool ret;
MultiThread *mt;
mt = new MultiThread();
mt->Test();
return (EXIT_SUCCESS);
}
====================================传说中的分割线=================================

PS:
1,还没有进行深入的测试,使用之前请进行必要的测试,以确保它符合你的要求。
2,如果你发现了 BUG,请联系我。

转载地址:/blog/item/de864d34f16b1a48241f1464.html
阅读(2109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~