多线程编程基础(线程创建)

转载自:http://blog.csdn.net/bertzhang/article/details/7219060

1、多线程的创建

线程的创建比较简单,先举一个例子热热身:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. void* Handler(void* param) {  
  4.   char* message = static_cast<char*>(param);  
  5.   printf("%s\n", message);  
  6. }  
  7. int main(int argc, char** argv) {  
  8.   pthread_t thread_id;  
  9.   char message[] = "hello world";  
  10.   pthread_create(&thread_id, NULL, Handler, message);  
  11.   void* ret = NULL;  
  12.   pthread_join(thread_id, &ret);  
  13. }  
有时候,我们需要将以个类的成员函数作为线程的routine函数使用,但pthread_create不能接收一个非静态的成员函数作为参数,因此类的成员函数必须是静态的,但这样的静态成员函数使得访问类的非静态数据成员会有问题,因此这不是一个很好的解决方案,例如下面的程序,ThreadBase类的设计和实现会有很多不便。

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. class ThreadBase {  
  4.  public:  
  5.   static void* Handler(void* param) {  
  6.     char* message = static_cast<char*>(param);  
  7.     printf("%s\n", message);  
  8.   }  
  9. };  
  10. int main(int argc, char** argv) {  
  11.   pthread_t thread_id;  
  12.   char message[] = "hello world";  
  13.   pthread_create(&thread_id, NULL, ThreadBase::Handler, message);  
  14.   void* ret = NULL;  
  15.   pthread_join(thread_id, &ret);  
  16. }  

解决方案有三个:

1)通过一个非成员函数包装一下这个类,非成员函数的参数是类的对象,有了类的对象就可一调用类的成员函数了,示例代码如下:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <string>  
  4. class ThreadBase {  
  5.  public:  
  6.   void SetMessage(const char* message) {  
  7.     message_ = message;  
  8.   }  
  9.   void Handler() {  
  10.     printf("%s\n", message_.c_str());  
  11.   }  
  12.  private:  
  13.   std::string message_;  
  14. };  
  15.   
  16. void* ThreadCall(void* object) {  
  17.   ThreadBase* thread_base = static_cast<ThreadBase*>(object);  
  18.   thread_base->Handler();  
  19. }  
  20. int main(int argc, char** argv) {  
  21.   pthread_t thread_id;  
  22.   char message[] = "hello world";  
  23.   ThreadBase thread_base;  
  24.   thread_base.SetMessage(message);  
  25.   pthread_create(&thread_id, NULL, ThreadCall, &thread_base);  
  26.   void* ret = NULL;  
  27.   pthread_join(thread_id, &ret);  
  28. }  

2)当然这个非成员函数也可以是类的静态成员函数,那么结果将是下面这个样式:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <string>  
  4. class ThreadBase {  
  5.  public:  
  6.   void SetMessage(const char* message) {  
  7.     message_ = message;  
  8.   }  
  9.   void Handler() {  
  10.     printf("%s\n", message_.c_str());  
  11.   }  
  12.   static void* ThreadCall(void* object) {  
  13.   ThreadBase* thread_base = static_cast<ThreadBase*>(object);  
  14.   thread_base->Handler();  
  15.   }  
  16.  private:  
  17.   std::string message_;  
  18. };  
  19.   
  20. int main(int argc, char** argv) {  
  21.   pthread_t thread_id;  
  22.   char message[] = "hello world";  
  23.   ThreadBase thread_base;  
  24.   thread_base.SetMessage(message);  
  25.   pthread_create(&thread_id, NULL, ThreadBase::ThreadCall, &thread_base);  
  26.   void* ret = NULL;  
  27.   pthread_join(thread_id, &ret);  
  28. }  


3)另线程创建的工作在类的内部完成,使得类具有对立运行的性质,熟悉java的同学对下面的实现一定非常熟悉:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <string>  
  4. class ThreadBase {  
  5.  public:  
  6.   virtual ~ThreadBase() {}  
  7.   void SetMessage(const char* message) {  
  8.     message_ = message;  
  9.   }  
  10.   void Start() {  
  11.     pthread_create(&thread_id_, NULL, Hook, this);  
  12.   }  
  13.   void* Join() {  
  14.     void* ret = NULL;  
  15.     pthread_join(thread_id_, &ret);  
  16.     return ret;  
  17.   }  
  18.   virtual void Run() {  
  19.     printf("%s\n", message_.c_str());  
  20.   }  
  21.  private:  
  22.   static void* Hook(void* object) {  
  23.     ThreadBase* thread_base= static_cast<ThreadBase*>(object);  
  24.     thread_base->Run();  
  25.   }  
  26.   pthread_t thread_id_;  
  27.   std::string message_;  
  28. };  
  29. class ThreadDerived : public ThreadBase {  
  30.  public:  
  31.   virtual void Run() {  
  32.     printf("a new derived multi-thread object is running\n");  
  33.   }  
  34. };  
  35. int main(int argc, char** argv) {  
  36.   pthread_t thread_id;  
  37.   char message[] = "hello world";  
  38.   ThreadBase thread_base;  
  39.   thread_base.SetMessage(message);  
  40.   thread_base.Start();  
  41.   thread_base.Join();  
  42.   ThreadDerived thread_derived;  
  43.   thread_derived.Start();  
  44.   thread_derived.Join();  
  45. }  

有了这个ThreadBase基类,再创建支持多线程的类就非常容易了,只要继承并实现Run函数就可以了。




参考文献:

https://computing.llnl.gov/tutorials/pthreads/

http://www.dutor.net/index.php/2011/11/pthread-in-cpp-class/

http://stackoverflow.com/questions/1151582/pthread-function-from-a-class


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值