java thread 头文件_Linux 下 C++ 的多线程基类 - Thread

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

下面是我写的基类,把代码保存在名为 Thread.h 的头文件中。

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

/*

* File: Thread.h

* Author: Null

* Blog: http://hi.baidu.com/hetaoos

* Created on 2008年7月30日, 上午10:13

*/

/*

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

*

*/#ifndef_THREAD_H

#define _THREAD_H

#include#include/*

* 线程运行实体类

*/classRunnable

{public://运行实体virtual voidrun() =0;

};/*

* 线程类

*

*/classThread :publicRunnable

{private://线程初始化序号static intthreadInitNumber;//当前线程初始化序号intcurThreadInitNumber;//线程体Runnable *target;//当前线程的线程IDpthread_t tid;//线程的状态intthreadStatus;//线程属性pthread_attr_t attr;//线程优先级sched_param param;//获取执行方法的指针static void*run0(void*pVoid);//内部执行方法void*run1();//获取一个线程序号static intgetNextThreadNum();public://线程的状态-新建static const intTHREAD_STATUS_NEW =0;//线程的状态-正在运行static const intTHREAD_STATUS_RUNNING =1;//线程的状态-运行结束static const intTHREAD_STATUS_EXIT = -1;//构造函数Thread();//构造函数Thread(Runnable *iTarget);//析构~Thread();//线程的运行实体voidrun();//开始执行线程boolstart();//获取线程状态intgetState();//等待线程直至退出voidjoin();//等待线程退出或者超时voidjoin(unsigned longmillisTime);//比较两个线程时候相同,通过 curThreadInitNumber 判断bool operator==(constThread *otherThread);//获取This线程IDpthread_t getThreadID();//获取当前线程IDstaticpthread_t getCurrentThreadID();//当前线程是否和某个线程相等,通过 tid 判断static boolisEquals(Thread *iTarget);//设置线程的类型:绑定/非绑定voidsetThreadScope(boolisSystem);//获取线程的类型:绑定/非绑定boolgetThreadScope();//设置线程的优先级,1-99,其中99为实时;意外的为普通voidsetThreadPriority(intpriority);//获取线程的优先级intgetThreadPriority();

};#endif /* _THREAD_H */intThread::threadInitNumber =1;intThread::getNextThreadNum()

{returnthreadInitNumber++;

}void*Thread::run0(void*pVoid)

{Thread*p = (Thread*)pVoid;p->run1();returnp;

}void*Thread::run1()

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

}voidThread::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);

}boolThread::start()

{returnpthread_create(&tid, &attr,run0,this) ==0;

}pthread_t Thread::getCurrentThreadID()

{returnpthread_self();

}pthread_t Thread::getThreadID()

{returntid;

}intThread::getState()

{returnthreadStatus;

}voidThread::join()

{if(tid >0)

{pthread_join(tid,NULL);

}

}voidThread::join(unsigned longmillisTime)

{if(tid ==0)

{return;

}if(millisTime ==0)

{join();

}else{unsigned longk =0;while(threadStatus !=THREAD_STATUS_EXIT &&k <=millisTime)

{usleep(100);k++;

}

}

}boolThread::operator==(constThread *otherThread)

{if(otherThread ==NULL)

{return false;

}if(curThreadInitNumber == (*otherThread).curThreadInitNumber)

{return true;

}return false;

}boolThread::isEquals(Thread *iTarget)

{if(iTarget ==NULL)

{return false;

}returnpthread_self() ==iTarget->tid;

}voidThread::setThreadScope(boolisSystem)

{if(isSystem)

{pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);

}else{pthread_attr_setscope(&attr,PTHREAD_SCOPE_PROCESS);

}

}boolThread::getThreadScope()

{intscopeType =0;pthread_attr_getscope(&attr, &scopeType);returnscopeType ==PTHREAD_SCOPE_SYSTEM;

}voidThread::setThreadPriority(intpriority)

{pthread_attr_getschedparam(&attr, &param);param.__sched_priority =priority;pthread_attr_setschedparam(&attr, &param);

}intThread::getThreadPriority()

{pthread_attr_getschedparam(&attr, &param);returnparam.__sched_priority;

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

具体看下面的示例代码。

呵呵,用法和 Java 的 Thread 很相似吧。

下面一共生成了3个线程,3个线程都共享 MultiThread::run()

在 run() 里面通过判断线程的实体来分离各个线程的工作。

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

/*

* File: newmain.cc

* Author: Null

* Blog: http://hi.baidu.com/hetaoos

* Created on 2008年7月30日, 上午12:49

*/#include"Thread.h"#includeclassMultiThread :publicThread

{public:Thread *th1;Thread *th2;voidTest()

{th1 =newThread(this);th1->setThreadPriority(99);th2 =newThread(this);start();th1->start();th2->start();th1->join();th2->join();

}voidrun()

{//Thread->isEquals(th1)if(Thread::isEquals(th1))

{intnumber =100;for(inti =0;i <10;i++)

{cout <getThreadPriority() <

}

}else if(Thread::isEquals(th2))

{intnumber =200;for(inti =0;i <10;i++)

{cout <getThreadPriority() <

}

}else if(Thread::isEquals(this))

{intnumber =300;for(inti =0;i <10;i++)

{cout <getThreadPriority() <

}

}

}

};/*

*

*/intmain(intargc,char**argv)

{boolret;MultiThread *mt;mt =newMultiThread();mt->Test();return(EXIT_SUCCESS);

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

PS:

1,还没有进行深入的测试,使用之前请进行必要的测试,以确保它符合你的要求。

2,如果你发现了 BUG,请联系我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值