【C++新经典】【并发与多线程】4 创建多个线程、数据共享问题分析与案例代码

本文介绍了使用C++和STL库如何创建和管理10个线程执行`sub_thread`函数,展示了线程ID的输出。重点在于理解线程创建和同步的基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <thread>
#include <memory>
#include <vector>

using namespace std;

void sub_thread() {
	cout << " thread id " << this_thread::get_id() << endl;
}

int main() {
	std::vector<thread> thread_vec;

	for (int i = 0; i < 10; i++) {
		thread_vec.push_back(thread(sub_thread));
	}

	for (int i = 0; i < 10; i++) {
		thread_vec[i].join();
	}

	return 0;
}

(base) ~/Desktop$ ./main
 thread id  thread id  thread id  thread id  thread id 0x7000064870000x70000650a000 thread id 0x7000069220000x70000689f000

0x7000066100000x70000658d000

 thread id 
 thread id 0x7000067160000x70000681c000

 thread id 0x700006799000
 thread id 0x700006693000

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值