标签:style blog color os io strong ar art div
方法一: 代理线程函数(proxyThreadFunc)作为类的静态成员函数, 回调函数指针作为类的私有成员变量
方法二: 代理线程函数(proxyThreadFunc)作为全局函数, 回调函数指针作为自定义结构体的成员
Noncopyable.h:
1 #ifndef _Noncopyable_H_ 2 #define _Noncopyable_H_ 3 4 class Noncopyable 5 { 6 public: 7 Noncopyable(){} 8 ~Noncopyable(){} 9 10 private: 11 Noncopyable(const Noncopyable & a){} 12 void operator=(const Noncopyable & a){} 13 }; 14 15 #endif
Thread.h:
1 #ifndef _Thread_H_ 2 #define _Thread_H_ 3 4 #include "Noncopyable.h" 5 #include <functional> 6 #include <pthread.h> 7 8 /*==================== 9 ====================*/ 10 11 class Thread : public Noncopyable 12 { 13 public: 14 typedef std::function<void ()> ThreadFunc; 15 Thread(ThreadFunc func) 16 :thread_id(0), running_status(-1), callback_func(func) 17 {} 18 virtual ~Thread() 19 { 20 if(!running_status) 21 pthread_detach(thread_id); 22 } 23 24 virtual void start() = 0; 25 virtual void join() = 0; 26 27 protected: 28 pthread_t thread_id; 29 int running_status; 30 ThreadFunc callback_func; 31 }; 32 33 /*==================== 34 ====================*/ 35 36 class ThreadStruct : public Thread 37 { 38 public: 39 ThreadStruct(ThreadFunc func) 40 :Thread(func) 41 {} 42 ~ThreadStruct(){} 43 44 virtual void start() override; 45 virtual void join() override; 46 }; 47 48 /*==================== 49 ====================*/ 50 51 class ThreadStatic : public Thread 52 { 53 public: 54 ThreadStatic(ThreadFunc func) 55 :Thread(func) 56 {} 57 ~ThreadStatic(){} 58 59 virtual void start() override; 60 virtual void join() override; 61 62 private: 63 static void * proxyThreadFunc(void *); 64 }; 65 66 #endif
Thread.cpp:
1 #include "Thread.h" 2 3 /*==================== 4 ====================*/ 5 6 struct ThreadData 7 { 8 typedef Thread::ThreadFunc ThreadFunc; 9 ThreadFunc cbfunc; 10 11 ThreadData(ThreadFunc func) 12 :cbfunc(func) 13 {} 14 15 void runThread() 16 { 17 cbfunc(); 18 } 19 }; 20 21 void * proxyThreadFunc(void * arg) 22 { 23 ThreadData * pd = NULL; 24 pd = static_cast<ThreadData *>(arg); 25 pd->runThread(); 26 delete pd; 27 return NULL; 28 } 29 30 void ThreadStruct::start() 31 { 32 ThreadData * td = NULL; 33 td = new ThreadData(callback_func); 34 if(pthread_create(&thread_id, NULL, proxyThreadFunc, td) < 0) 35 delete td; 36 } 37 38 void ThreadStruct::join() 39 { 40 pthread_join(thread_id, NULL); 41 } 42 43 /*==================== 44 ====================*/ 45 46 void * ThreadStatic::proxyThreadFunc(void * arg) 47 { 48 ThreadStatic * pd = NULL; 49 pd = static_cast<ThreadStatic *>(arg); 50 pd->callback_func(); 51 return NULL; 52 } 53 54 void ThreadStatic::start() 55 { 56 pthread_create(&thread_id, NULL, proxyThreadFunc, this); 57 } 58 59 void ThreadStatic::join() 60 { 61 pthread_join(thread_id, NULL); 62 }
ThreadMain.cpp:
1 #include "Thread.h" 2 3 #include <iostream> 4 #include <stdlib.h> 5 #include <sys/socket.h> 6 #include <unistd.h> 7 8 using namespace std; 9 10 class Test 11 { 12 public: 13 void foo() 14 { 15 cout << "foo()" << endl; 16 } 17 18 void bar(int i) 19 { 20 cout << "bar(" << i << ")" << endl; 21 } 22 }; 23 24 /*==================== 25 ====================*/ 26 27 class TestThread 28 { 29 public: 30 TestThread() 31 :cnt(0), pThread(NULL) 32 {} 33 34 void threadFunc() 35 { 36 while(cnt < 5) 37 { 38 sleep(1); 39 cout << "cnt = " << ++cnt << endl; 40 } 41 } 42 43 void start() 44 { 45 pThread->start(); 46 } 47 48 void join() 49 { 50 pThread->join(); 51 } 52 53 void attach(Thread * th) 54 { 55 pThread = th; 56 } 57 58 private: 59 int cnt; 60 Thread * pThread; //类的组合 61 }; 62 63 /*==================== 64 ====================*/ 65 66 int main() 67 { 68 Thread * p1, * p2; 69 TestThread tt; 70 tt.attach(new ThreadStatic(bind(&TestThread::threadFunc, &tt))); 71 Test test; 72 p1 = new ThreadStruct(bind(&Test::foo, &test)); 73 p2 = new ThreadStatic(bind(&Test::bar, &test, 10)); 74 75 tt.start(); 76 p1->start(); 77 p2->start(); 78 tt.join(); 79 p1->join(); 80 p2->join(); 81 delete p1; 82 delete p2; 83 }
封装Thread的两种方法 via C++ in Linux
标签:style blog color os io strong ar art div
原文地址:http://www.cnblogs.com/ericxilix/p/3954384.html