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

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();

相关文章:

  • 【Hive入门】Hive高级特性:事务表与ACID特性详解
  • 大数据平台与数据仓库的核心差异是什么?
  • openharmony 4.1 运行busybox工具包(保姆教程)
  • encodeURI和encodeURICompoent的区别
  • 云备份服务器,数据备份服务器的方法有哪些?
  • 配置和使用基本存储
  • Golang - 实现文件管理服务器
  • 如何用AI生成个人职业照/西装照?
  • SALOME源码分析: SMESH模块
  • 17、商品管理:魔药商店运营——React 19 CRUD实现
  • React 后台管理系统
  • 涨薪技术|0到1学会性能测试第43课-apache status模块监控
  • SPL 量化 回测
  • 论文阅读:2024 arxiv Jailbreaking Black Box Large Language Models in Twenty Queries
  • python合并word中的run
  • Ubuntu ZLMediakit的标准配置文件(rtsp->rtmp->hls)
  • 《分词算法大揭秘:BPE、BBPE、WordPiece、ULM常见方法介绍》
  • 在原生代码(非webpack)里使用iview的注意事项
  • 回归分析丨基于R语言复杂数据回归与混合效应模型【多水平/分层/嵌套】技术与代码
  • AI预测3D新模型百十个定位预测+胆码预测+去和尾2025年4月30日第68弹
  • 中国海警位中国黄岩岛领海及周边区域执法巡查
  • 民营经济促进法出台,自今年5月20日起施行
  • 浙商银行外部监事高强无法履职:已被查,曾任建行浙江省分行行长
  • 民生访谈|规范放生活动、提升供水品质……上海将有这些举措
  • 杨国荣︱学术上的立此存照——《故旧往事,欲说还休》读后
  • 贸促会答澎湃:5月22日将举办2025年贸易投资促进峰会