同时进行的任务 · 并行与并发库
#include <iostream>
#include <thread>
void hello()
{
std::cout << "Hello from thread!" << std::endl;
}
void greet(const std::string& name)
{
std::cout << "Hello, " << name << "!" << std::endl;
}
int main()
{
// 创建线程
std::thread t1(hello);
// 带参数的线程
std::thread t2(greet, "World");
// Lambda 线程
std::thread t3([]() {
std::cout << "Hello from lambda!" << std::endl;
});
// 等待线程完成
t1.join();
t2.join();
t3.join();
return 0;
}工具
用途
Last updated