linux C++ 面向对象线程类封装

来源:互联网 发布:邦奇-威尔斯 知乎 编辑:程序博客网 时间:2024/06/05 11:47

转自  : http://blog.csdn.net/maotoula/article/details/18501963


1.封装遇到的问题

pthread线程封装为抽象类,这样用户在使用线程时,只需要继承一下这个抽象类,并实现相应的接口就可以了。这样做的好处是用户可以将注意力集中在线程所要执行的逻辑上,而不需要关注创建线程、销毁线程等细节问题上。

我们抽象类的名称为Thread,其中有一个成员函数run,该函数为的声明形式为:

void run() = 0;

即将该成员函数声明为纯虚函数,用户继承此类必须要实现此成员函数。Thread中还有另外一个成员函数start,该函数的声明形式为:

void start();

用户在子类中调用start方法,将启动线程,并在线程中执行run函数。

最常想到的方法就是在start方法中使用pthread_create创建一个线程,并调用run函数。如下面这样的实现:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void start()   
  2. {   
  3.     int status;  
  4.    
  5.     status = pthread_create(_pThread,NULL,Thread::run,NULL);   
  6.     if(status != 0)   
  7.         err_abort(“creating thread failure”,status);   
  8.    
  9.  }   


这样编译肯定是不能通过的,这是因为pthread_create要求的线程例程的接口形式为:

void *(*thread_routin)(void *args);

而上面代码中提供的线程例程的接口形式为:

void Thread::run()

显然不符合要求的接口。

为了能够在start中调用run函数,我们不得不采用一种迂回的方式。下面提供两种方法:一种是使用静态成员函数,另外一种是使用友元函数。

       静态成员函数的作用域是全局的,而不仅仅局限于某个函数中。静态成员函数的实现方法和C语言中的普通函数类似,因此静态函数没有this指针,静态函数只能操作静态成员变量。之所以将静态函数封装到类中,在很大程度上也只是为了满足面向对象的特性之一-----封装性。

2.使用静态函数

       需要特别注意的是mian函数中使用pthread_create的执行例程为MyThread类中的线程代理函数thread_proxy_func,在此函数中在调用run函数,这样就顺利的迂回到了run函数。基于这种方法,我们可以用静态函数来封装一个简单的抽象类,以下为封装的代码,由三个文件构成:Thread.h(类的声明文件),Thread.cpp(类的实现文件),main.cpp(测试文件):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef THREAD_H  
  2. #define THREAD_H  
  3. #include <iostream>  
  4. #include <pthread.h>  
  5.   
  6. using namespace std;  
  7.   
  8. class Thread  
  9. {  
  10. private:  
  11.     //当前线程的线程ID  
  12.     pthread_t tid;  
  13.     //线程的状态  
  14.     int threadStatus;  
  15.     //获取执行方法的指针  
  16.     static void* run0(void* pVoid);  
  17.     static void * thread_proxy_func(void * args);  
  18.     //内部执行方法  
  19.     void* run1();  
  20. public:  
  21.     //线程的状态-新建  
  22.     static const int THREAD_STATUS_NEW = 0;  
  23.     //线程的状态-正在运行  
  24.     static const int THREAD_STATUS_RUNNING = 1;  
  25.     //线程的状态-运行结束  
  26.     static const int THREAD_STATUS_EXIT = -1;  
  27.     //构造函数  
  28.     Thread();  
  29.     //线程的运行实体  
  30.     virtual void run()=0;  
  31.     //开始执行线程  
  32.     bool start();  
  33.     //获取线程ID  
  34.     pthread_t getThreadID();  
  35.     //获取线程状态  
  36.     int getState();  
  37.     //等待线程直至退出  
  38.     void join();  
  39.     //等待线程退出或者超时  
  40.     void join(unsigned long millisTime);  
  41. };  
  42.   
  43. class MultiThread : public Thread  
  44. {  
  45. public:  
  46.     void run()  
  47.     {  
  48.         int number = 0;  
  49.         for (int i = 0; i < 10; i++)  
  50.         {  
  51.             cout << "Current number is " << number++;  
  52.             cout << " PID is " << getpid() << " TID is " << getThreadID() << endl;  
  53.             sleep(1);  
  54.         }  
  55.     }  
  56. };  
  57.   
  58. #endif  
Thread.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "thread.h"  
  2.   
  3. void* Thread::run0(void* pVoid)  
  4. {  
  5.     Thread* p = (Thread*) pVoid;  
  6.     p->run1();  
  7.     return p;  
  8. }  
  9.   
  10. void* Thread::run1()  
  11. {  
  12.     threadStatus = THREAD_STATUS_RUNNING;  
  13.     tid = pthread_self();  
  14.     run();  
  15.     threadStatus = THREAD_STATUS_EXIT;  
  16.     tid = 0;  
  17.     pthread_exit(NULL);  
  18. }  
  19.   
  20. Thread::Thread()  
  21. {  
  22.     tid = 0;  
  23.     threadStatus = THREAD_STATUS_NEW;  
  24. }  
  25.   
  26. bool Thread::start()  
  27. {  
  28.         int iRet = 0;  
  29.     pthread_create(&tid, NULL, thread_proxy_func, this) == 0;  
  30. }  
  31.   
  32. pthread_t Thread::getThreadID()  
  33. {  
  34.     return tid;  
  35. }  
  36.   
  37. int Thread::getState()  
  38. {  
  39.     return threadStatus;  
  40. }  
  41.   
  42. void Thread::join()  
  43. {  
  44.     if (tid > 0)  
  45.     {  
  46.         pthread_join(tid, NULL);  
  47.     }  
  48. }  
  49. void * Thread::thread_proxy_func(void * args)  
  50. {  
  51.         Thread * pThread = static_cast<Thread *>(args);   
  52.    
  53.         pThread->run();   
  54.           
  55.         return NULL;   
  56. }  
  57.   
  58. void Thread::join(unsigned long millisTime)  
  59. {  
  60.     if (tid == 0)  
  61.     {  
  62.         return;  
  63.     }  
  64.     if (millisTime == 0)  
  65.     {  
  66.         join();  
  67.     }else  
  68.     {  
  69.         unsigned long k = 0;  
  70.         while (threadStatus != THREAD_STATUS_EXIT && k <= millisTime)  
  71.         {  
  72.             usleep(100);  
  73.             k++;  
  74.         }  
  75.     }  
  76. }  
main.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <pthread.h>  
  3. #include "thread.h"  
  4.   
  5. using namespace std;  
  6.   
  7. int main(int argv,char *argc)  
  8. {  
  9.     MultiThread tt;  
  10.     tt.start();  
  11.     tt.join();  
  12.     return 0;  
  13. }  

3.使用友元函数

       友元函数的作用和静态函数相同,都起到一个代理的作用。需要将对象的指针作为参数传递给这个友元函数,然后在友元函数中调用run函数。代码如下,

由三个文件构成:Thread.h(类的声明文件),Thread.cpp(类的实现文件),main.cpp(测试文件):

Thread.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef THREAD_H  
  2. #define THREAD_H  
  3. #include <iostream>  
  4. #include <pthread.h>  
  5.   
  6. using namespace std;  
  7.   
  8. class Thread  
  9. {  
  10. private:  
  11.     //当前线程的线程ID  
  12.     pthread_t tid;  
  13.     //线程的状态  
  14.     int threadStatus;  
  15.     //获取执行方法的指针  
  16.     static void* run0(void* pVoid);  
  17.     //static void * thread_proxy_func(void * args);  
  18.     friend void * thread_proxy_func(void * args);  
  19.     //内部执行方法  
  20.     void* run1();  
  21. public:  
  22.     //线程的状态-新建  
  23.     static const int THREAD_STATUS_NEW = 0;  
  24.     //线程的状态-正在运行  
  25.     static const int THREAD_STATUS_RUNNING = 1;  
  26.     //线程的状态-运行结束  
  27.     static const int THREAD_STATUS_EXIT = -1;  
  28.     //构造函数  
  29.     Thread();  
  30.     //线程的运行实体  
  31.     virtual void run()=0;  
  32.     //开始执行线程  
  33.     bool start();  
  34.     //获取线程ID  
  35.     pthread_t getThreadID();  
  36.     //获取线程状态  
  37.     int getState();  
  38.     //等待线程直至退出  
  39.     void join();  
  40.     //等待线程退出或者超时  
  41.     void join(unsigned long millisTime);  
  42. };  
  43.   
  44. class MultiThread : public Thread  
  45. {  
  46. public:  
  47.     void run()  
  48.     {  
  49.         int number = 0;  
  50.         for (int i = 0; i < 10; i++)  
  51.         {  
  52.             cout << "Current number is " << number++;  
  53.             cout << " PID is " << getpid() << " TID is " << getThreadID() << endl;  
  54.             sleep(1);  
  55.         }  
  56.     }  
  57. };  
  58.   
  59. #endif  

Thread.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "thread.h"  
  2.   
  3. void* Thread::run0(void* pVoid)  
  4. {  
  5.     Thread* p = (Thread*) pVoid;  
  6.     p->run1();  
  7.     return p;  
  8. }  
  9.   
  10. void* Thread::run1()  
  11. {  
  12.     threadStatus = THREAD_STATUS_RUNNING;  
  13.     tid = pthread_self();  
  14.     run();  
  15.     threadStatus = THREAD_STATUS_EXIT;  
  16.     tid = 0;  
  17.     pthread_exit(NULL);  
  18. }  
  19.   
  20. Thread::Thread()  
  21. {  
  22.     tid = 0;  
  23.     threadStatus = THREAD_STATUS_NEW;  
  24. }  
  25.   
  26. bool Thread::start()  
  27. {  
  28.         int iRet = 0;  
  29.     pthread_create(&tid, NULL, thread_proxy_func, this) == 0;  
  30. }  
  31.   
  32. pthread_t Thread::getThreadID()  
  33. {  
  34.     return tid;  
  35. }  
  36.   
  37. int Thread::getState()  
  38. {  
  39.     return threadStatus;  
  40. }  
  41.   
  42. void Thread::join()  
  43. {  
  44.     if (tid > 0)  
  45.     {  
  46.         pthread_join(tid, NULL);  
  47.     }  
  48. }  
  49. void * thread_proxy_func(void * args)  
  50. {  
  51.         Thread * pThread = static_cast<Thread *>(args);   
  52.    
  53.         pThread->run();   
  54.           
  55.         return NULL;   
  56. }  
  57.   
  58. void Thread::join(unsigned long millisTime)  
  59. {  
  60.     if (tid == 0)  
  61.     {  
  62.         return;  
  63.     }  
  64.     if (millisTime == 0)  
  65.     {  
  66.         join();  
  67.     }else  
  68.     {  
  69.         unsigned long k = 0;  
  70.         while (threadStatus != THREAD_STATUS_EXIT && k <= millisTime)  
  71.         {  
  72.             usleep(100);  
  73.             k++;  
  74.         }  
  75.     }  
  76. }  

main.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "thread.h"  
  3.   
  4. using namespace std;  
  5.   
  6. int main(int argv,char *argc)  
  7. {  
  8.     MultiThread tt;  
  9.     tt.start();  
  10.     tt.join();  
  11.     return 0;  
  12. }  

运行结果



makefile参考

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ANAME=server  
  2. CC=g++  
  3. TMP_PROGS = main.cpp thread.cpp  
  4. PROGS = $(TMP_PROGS)  
  5. OBJS = $(PROGS:.cpp=.o)  
  6. INCDIR=./  
  7.   
  8. all: $(ANAME)  
  9.   
  10. $(ANAME): $(OBJS)  
  11.     @echo "--------------- .o to ELT "  
  12.     $(CC) -g $(TMP_PROGS) -o $@ -lpthread   
  13. .cpp.o:  
  14.     @echo "--------------- CPP to .o "  
  15.     $(CC) -g $(CFLAGS) -I$(INCDIR) -c  $< -o $@  -lpthread   
  16.   
  17. clean:  
  18.     $(RM) $(ANAME)  
  19.     $(RM) *.o  


0 0
原创粉丝点击