当前位置: 首页 > news >正文

【Linux】多线程_2

文章目录

  • 九、多线程
    • 2. 线程的控制
  • 未完待续


九、多线程

2. 线程的控制

主线程退出 等同于 进程退出 等同于 所有线程都退出。为了避免主线程退出,但是新线程并没有执行完自己的任务的问题,主线程同样要跟进程一样等待新线程返回。
在这里插入图片描述
pthread_join 函数则可以等待新线程返回。
Makefile

test_thread: testThread.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY: clean
clean:rm -f test_thread

testThread.cc

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;// 新线程
void* newpthreadrun(void* arg)
{int cnt = 5;while (cnt--){cout << "I am newpthreadrun thread" << endl;sleep(1);}
}int main()
{pthread_t tid;pthread_create(&tid, nullptr, newpthreadrun, nullptr);// 主线程pthread_join(tid, nullptr);cout << "I am main thread" << endl;sleep(5);return 0;
}

在这里插入图片描述
多线程中,任何一个线程出现异常,都会导致整个进程退出 ---- 多线程代码往往健壮性不好。 不能用 exit() 来退出线程,因为 exit() 是退出进程的,可以使用 pthread_exit() 来退出线程,也可以在主线程中使用 pthread_cancel() 来取消新线程线程,新线程会返回 -1。
我们来创建一个多线程:
Makefile

test_thread: testThread.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY: clean
clean:rm -f test_thread

testThread.cc

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;const int threadnum = 5;// 加法任务
class Task
{
public:Task(int x, int y):datax(x),datay(y){}int Excute(){return datax + datay;}~Task() {}
private:int datax;int datay;
};// 线程数据
class ThreadData
{
public:ThreadData(int x, int y, const string& threadname):t(x, y),threadname(threadname){}string Threadname(){return threadname;}int run(){return t.Excute();}~ThreadData() {}
private:string threadname;Task t;
};// 执行任务后的结果
class Result
{
public:Result() {}void SetResult(int result, const string& threadname){_result = result;_threadname = threadname;}void Print(){cout << _threadname << " run result: " << _result << endl;}~Result() {}
private:int _result;string _threadname;
};// 线程执行任务
void* handlerTask(void* args)
{ThreadData* td = static_cast<ThreadData*>(args);string name = td->Threadname();Result* res = new Result();int result = td->run();res->SetResult(result, name);return res;
}int main()
{// 创建线程并分配任务vector<pthread_t> threads;for (int i = 0; i < threadnum; i++){char threadname[64];snprintf(threadname, 64, "Thread-%d", i + 1);ThreadData* td = new ThreadData(10, 20, threadname);pthread_t tid;pthread_create(&tid, nullptr, handlerTask, td);threads.push_back(tid);}// 等待线程执行完毕并获取结果vector<Result*> result_set;void* ret = nullptr;for (auto& tid : threads){pthread_join(tid, &ret);result_set.push_back((Result*)(ret));}// 打印结果for (auto& res : result_set){res->Print();delete res;}return 0;
}

执行结果:
在这里插入图片描述


未完待续

http://www.lryc.cn/news/396305.html

相关文章:

  • 58、基于径向基神经网络的曲线拟合(matlab)
  • 3.上传图片(阿里云空间,oss验证)
  • 仪表板展示|DataEase看中国:2023年中国新能源汽车经济运行情况分析
  • “Numpy数据分析与挖掘:高效学习重点技能“
  • 百川工作手机实现销售管理微信监控系统
  • RAG 工业落地方案框架(Qanything、RAGFlow、FastGPT、智谱RAG)细节比对!CVPR自动驾驶最in挑战赛赛道,全球冠军被算力选手夺走了
  • 华为防火墙 拓扑搭建1
  • Linux 利用命名空间创建一个自己的“容器“
  • RAG的学习与实践——LangChain和LlamaIndex学习笔记
  • Python爬虫原理以及3个小案例(源码)
  • Vagrant配合VirtualBox搭建虚拟机
  • Elasticsearch 建议(Suggesters):实现自动补全和拼写检查
  • 部署过docker后,防火墙firewall与iptables的基本指令
  • 华为 RIP 协议中 RIP 兼容版本、RIPv1、RIPv2 在收发 RIP 报文时的区别
  • 深度学习pytorch多机多卡网络配置桥接方法
  • 服务器信息获取工具
  • uniapp 防止重复提交数据
  • 线程池工具类
  • 印尼“支付宝” DANA 如何借力 OceanBase 实现3个“关键零”
  • 2018-2022 年份微博签到数据集
  • Avalonia开发实践(二)——开发带边框的Grid
  • Java泛型的定义与运用
  • Java如何自定义注解及在SpringBoot中的应用
  • 微软 Edge 浏览器全解析
  • C++ 八股(1)
  • 超高精电容传感器PCAP01调试+LABVIEW数据可视化调试手记
  • 5.更多
  • ConditionalOnJndi注解使用介绍、应用场景以及示例代码
  • Spring Cloud 引入
  • 自定义波形图View,LayoutInflater动态加载控件保存为本地图片