C++压力测试技巧汇总(4个实用方法) - #include <
使用多线程进行并发测试 拼多多帮助力入口
通过创建多个线程并让它们同时执行任务,可以模拟多核处理器的性能。例如:
#include <iostream>
#include <thread>
#include <vector>
int main() {
// 创建一个线程向量
std::vector<std::thread> threads;
// 创建并启动线程
for (int i = 0; i < 10; ++i) {
threads.emplace_back([]() {
// 在这里执行你的任务
std::cout << "Thread " << i << " is running." << std::endl;
});
}
// 等待所有线程完成
std::this_thread::sleep_for(std::chrono::seconds(1));
// 等待所有线程退出
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
return 0;
}