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

c++线程的创建

c++ 11 线程编程实战

目录

  • c++ 11 线程编程实战
  • 1,线程的创建
    • 1.1 传入无参函数
    • 1.2 传入有参函数
    • 1.3 传入类内部函数
    • 1.4 lambda表达式

1,线程的创建

1.1 传入无参函数

//传入函数,创建线程
void  ThreadMain()
{//获取线程IDstd::thread::id  thisthreadId = std::this_thread::get_id();cout << "begin thread main" << endl;
}
int  main()
{thread  th1(ThreadMain);th1.join();
}

1.2 传入有参函数

//传入带参数的函数,创建线程
void  ThreadMainCanshu(int a, int b)
{//获取线程IDstd::thread::id  thisthreadId = std::this_thread::get_id();cout << "begin ThreadMainCanshu" << endl;cout << "a : " << a << endl;cout << "b : " << b << endl;
}
int  main()
{thread  th2(ThreadMainCanshu, 1, 2);th2.join();
}

1.3 传入类内部函数

//传入类成员函数创建线程
class  MyThread
{
public:void Main(){//获取线程IDstd::thread::id  thisthreadId = std::this_thread::get_id();cout << "My Thread Main" << endl;thread th1(&MyThread::thcanshu, this, 10, 20);th1.detach();Sleep(10);}void  thcanshu(int a,int b){cout << "MyThread : a " << a << " b " << b << endl;}
};
void  dealth1()
{MyThread  myth;thread  th3(&MyThread::thcanshu, &myth, 10, 20);th3.join();
}void  dealth2()
{MyThread  myth;myth.Main();
}
int  main()
{dealth1();dealth2();
}

1.4 lambda表达式

std::thread  my_thread([]{cout<<"my thread"<<endl;
}
);
my_thread.join();
http://www.xdnf.cn/news/3002.html

相关文章:

  • Qwen3 开源!深度对比 DeepSeek,一文选对模型
  • vue3数字秒转换为时分秒格式
  • 西游记2:天花乱坠,地涌金莲;说一会道,讲一会禅,三家(指儒、释、道)配合本如然;长生不老之术、七十二般变化之能以及筋斗云之法;你从何处而来,便回到何处去吧
  • Linux基础篇、第一章_01_3安装虚拟机手动安装部署Ubuntu22.04服务器
  • MySQL日志详解
  • 算法训练营第五天 | 454.四数相加II\ 383. 赎金信\15. 三数之和\ 18. 四数之和
  • 同一个路由器接口eth0和ppp0什么不同?
  • PCB入门指南:从电阻到常见电路的全解析
  • acwing背包问题求方案数
  • NOC科普一
  • 大模型——使用coze搭建基于DeepSeek大模型的智能体实现智能客服问答
  • 你的私域该大扫除了
  • 【记录】Python调用大模型(以Deepseek和Qwen为例)
  • 思维导图的快速生成
  • 某铝制品长棒材精轧线低压无源滤波装置改造案例
  • 智慧停车场升级难题:免布线视频桩如何破解三大核心痛点
  • 低版的spring boot 1.X接入knife4j
  • 批量修改文件名前后缀
  • 国内无法访问GitHub官网的问题解决
  • Cell Res | Stereo-seq揭示人类肝癌浸润区促进肝细胞-肿瘤细胞串扰、局部免疫抑制和肿瘤进展
  • 探索数学之美:分形几何之在线交互式曼德博集合动画演示工具
  • C++类与对象基础
  • 破局传统采购,连锁大药房打造一体化招采平台
  • 川土微电子全国产供应链且全面通过IBEE EMC认证的车规CAN收发器CA-IF1044AX-Q1
  • Mysql数据类型
  • 0.5 像素边框实现
  • Javscript 数组的常用方法有哪些?
  • 软实时如Windows,在工业领域的弊端
  • Game Booster汉化版:一键优化,畅享游戏
  • std::functional 类是干什么用的?