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

本文介绍了在C++中创建多线程的三种方法:通过非成员函数包装类的成员函数;利用类的静态成员函数;在线程类内部实现线程创建。文中还提供了详细的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://blog.youkuaiyun.com/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


基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值