最近看了一个Thread类(忘记在哪里看的了),感觉不错。
创建线程时线程对应的函数必须是类的静态成员,由于静态成员无法访问类的非静态成员,我从前都是把对象的指针作为参数传递给线程函数来避免这个问题,但是在逻辑上线程函数还需要访问对象的私有成员,总是感觉代码很不优雅。这个Thread类同java中的Thread类在用法上比较类似,使用一种比较优雅的方法避免了上面的问题。使用时只要从Thread派生一个子类并实现线程运行的函数 void run() 就可以了。还是看代码吧:
先把使用方法贴上来:
下面是Thread类的实现,为了阅读清晰,删减了很多内容
创建线程时线程对应的函数必须是类的静态成员,由于静态成员无法访问类的非静态成员,我从前都是把对象的指针作为参数传递给线程函数来避免这个问题,但是在逻辑上线程函数还需要访问对象的私有成员,总是感觉代码很不优雅。这个Thread类同java中的Thread类在用法上比较类似,使用一种比较优雅的方法避免了上面的问题。使用时只要从Thread派生一个子类并实现线程运行的函数 void run() 就可以了。还是看代码吧:
先把使用方法贴上来:
C++语言:
Codee#880
01
#include<iostream>
02
03 #include "Thread.h"
04
05 class MyThreadClass : public Thread
06 {
07 private :
08 int a;
09 public :
10 MyThreadClass( )
02
03 #include "Thread.h"
04
05 class MyThreadClass : public Thread
06 {
07 private :
08 int a;
09 public :
10 MyThreadClass( )
{
11 a = 0;
12 }
13 ~ MyThreadClass (){ }
15 virtual void run();
16 };
17
18 void Receiver :: run ()
11 a = 0;
12 }
13 ~ MyThreadClass (){ }
15 virtual void run();
16 };
17
18 void Receiver :: run ()
{
19 a ++;
20 std :: cout << a << std :: endl;
21 }
22
23 int main( int argc , char * argv [])
24 {
25 MyThreadClass myThread;
26 myThread . start(); //创建了一个线程,运行函数run()
27 myThread . join(); //等待线程结束
28 return 0;
29 }
19 a ++;
20 std :: cout << a << std :: endl;
21 }
22
23 int main( int argc , char * argv [])
24 {
25 MyThreadClass myThread;
26 myThread . start(); //创建了一个线程,运行函数run()
27 myThread . join(); //等待线程结束
28 return 0;
29 }
下面是Thread类的实现,为了阅读清晰,删减了很多内容
C++语言: Thread.h
01
#ifndef COMMUNITCATE_H
02 #define COMMUNITCATE_H
03
04
05
06 #include "pthread.h"
07
08
09 class Thread
10 {
11 protected :
12 pthread_t _tid;
13 static void * run0( void * opt);
14 void * run1(); // 如果类中有保存线程状态的变量,可以 在这个函数中可以进行更改操作
15 public :
16 Thread();
17 ~ Thread();
18 /**
19 * 创建线程,线程函数是 run0
20 *
21 * @return 成功返回 ture 否则返回 false
22 */
23 bool start();
24 /**
25 * join this thread
26 *
27 */
28 void join();
29 virtual void run (){
30
31 }
32 };
33
34
35 #endif
02 #define COMMUNITCATE_H
03
04
05
06 #include "pthread.h"
07
08
09 class Thread
10 {
11 protected :
12 pthread_t _tid;
13 static void * run0( void * opt);
14 void * run1(); // 如果类中有保存线程状态的变量,可以 在这个函数中可以进行更改操作
15 public :
16 Thread();
17 ~ Thread();
18 /**
19 * 创建线程,线程函数是 run0
20 *
21 * @return 成功返回 ture 否则返回 false
22 */
23 bool start();
24 /**
25 * join this thread
26 *
27 */
28 void join();
29 virtual void run (){
30
31 }
32 };
33
34
35 #endif
C++语言: Thread.cpp
01
#include "Thread.h"
02
03 Thread :: Thread (){
04
05 }
06
07 Thread ::~ Thread (){
08
09 }
10
11
12
13 void * Thread :: run0( void * opt)
14 {
15 Thread * p = ( Thread *) opt;
16 p -> run1();
17 return p;
18 }
19
20 void * Thread :: run1()
21 {
22 _tid = pthread_self();
23 run();
24 _tid = 0;
25 pthread_exit( NULL);
26 }
27
28 bool Thread :: start()
29 {
30 return pthread_create( & _tid , NULL , run0 , this) == 0;
31 }
32
33 void Thread :: join()
34 {
35 if( _tid > 0 ){
36 pthread_join( _tid , NULL);
37 }
38 }
02
03 Thread :: Thread (){
04
05 }
06
07 Thread ::~ Thread (){
08
09 }
10
11
12
13 void * Thread :: run0( void * opt)
14 {
15 Thread * p = ( Thread *) opt;
16 p -> run1();
17 return p;
18 }
19
20 void * Thread :: run1()
21 {
22 _tid = pthread_self();
23 run();
24 _tid = 0;
25 pthread_exit( NULL);
26 }
27
28 bool Thread :: start()
29 {
30 return pthread_create( & _tid , NULL , run0 , this) == 0;
31 }
32
33 void Thread :: join()
34 {
35 if( _tid > 0 ){
36 pthread_join( _tid , NULL);
37 }
38 }