该博客通过观看b站视频 [C++ 多线程并发 基础入门教程] 1.1 创建线程(thread) 进行的笔记总结,up主讲的非常好,适合了解一点多线程知识,想快速入门的同学
使用总结:
| 使用 | 解释 |
|---|---|
| std::thread thread1(func, a) | 创建一个线程并运行 |
| thread1.join() | 等待子线程thread1 |
| thread1.detach() | 当前线程与子线程分离 |
| thread1.get_id() | 获取thread1线程的id |
| thread1.hardware_concurrency() | 获取硬件支持的线程数量 |
| std::this_thread::sleep_for() | 使线程等待 |
#include#include void func(int a) { while (true) { std::cout << "child thread a = "< int a = 10; // 创建一个线程并运行 std::thread thread1(func, a); // 打印thread1线程的id std::cout << "thread id =" << thread1.get_id() << std::endl; std::cout << "support num =" << thread1.hardware_concurrency() << std::endl; // 等待子线程thread1(阻塞该线程) thread1.join(); // 分离子线程thread1(不推荐使用) //thread1.detach(); }



