C++多线程6

2021/11/21 20:12:32

本文主要是介绍C++多线程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 }

 



这篇关于C++多线程6的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程