C++多线程-chap1多线程入门6

这里,只是记录自己的学习笔记。

顺便和大家分享多线程的基础知识。然后从入门到实战。有代码。

知识点来源:

https://edu.51cto.com/course/26869.html


 

lambda临时函数作为线程入口函数

复制代码
 1 #include <iostream>
 2 #include <string>
 3 #include <thread>
 4 
 5 using namespace std;
 6 
 7 //lambda临时函数作为线程入口函数
 8 
 9 
10 //线程入口为类成员 lambda 函数
11 class TestLambda{
12 public:
13     void Start() {
14         thread th([this]() {cout << "name= " << name<< endl; });
15         th.detach();
16     }
17 
18     string name = "test lambda";
19 };
20 
21 int main() {
22 
23     // 用 lambda 表达式作为线程入口函数,并接收一个参数
24     thread  th(
25         [](int i) {
26         cout << "lambda i:" << i << endl;
27     }, 666);
28     th.join();
29 
30 
31     TestLambda test;
32     test.Start();
33 
34 
35 
36     getchar();
37     return 0;
38 }
复制代码

 

posted @   He_LiangLiang  阅读(32)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-11-21 sockaddr和sockaddr_in的区别
2019-11-21 CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)
2019-11-21 telnet: Unable to connect to remote host: Connection refused
2019-11-21 telnet: Unable to connect to remote host: No route to host
2019-11-21 IP地址转换函数
2019-11-21 Linux 网络通信 API详解【转载】
2019-11-21 linux 文件详细信息
点击右上角即可分享
微信分享提示