#include <iostream>
#include <thread>
// 定义一个线程函数
void threadFunction(int id) {
// 线程执行的任务
std::cout << "Thread ID: " << id << " is running." << std::endl;
}
int main() {
// 创建线程
std::thread t1(threadFunction, 1);
std::thread t2(threadFunction, 2);
// 等待线程完成
t1.join();
t2.join();
std::cout << "Both threads have finished execution." << std::endl;
return 0;
}
#include <thread> 引入了 C++ 标准库中的线程支持。void threadFunction(int id) 是一个简单的函数,它接收一个整数参数 id 并输出一条信息。std::thread 类创建两个线程 t1 和 t2,分别传递 threadFunction 函数和参数 1 和 2。join() 方法确保主线程等待子线程完成后再继续执行。下一篇:c++函数调用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站