写一个简单的多线程Cpp代码
时间: 2023-12-01 21:55:24 浏览: 64
C++简单多线程
Sure, here is a simple multi-threaded C++ code:
```
#include <iostream>
#include <thread>
using namespace std;
void printHello() {
cout << "Hello from thread " << this_thread::get_id() << endl;
}
int main() {
thread t1(printHello);
thread t2(printHello);
t1.join();
t2.join();
return 0;
}
```
This code creates two threads using the `thread` class and passes a function `printHello` to each thread. Each thread will print a message indicating which thread it is running on using the `this_thread::get_id()` function. The `join()` method is used to wait for the threads to finish executing before the program exits.
Hope that helps!
阅读全文