C++之多线程
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void thread_func() {
cout << "thread func" << endl;
}
class Thread {
public:
void thread_function();
};
void Thread::thread_function() {
cout << "this is a child thread function!" << endl;
}
int main() {
thread t1(thread_func);
cout << "t1_thread_id:" << t1.get_id() << endl;
thread t2(move(t1));
cout << "child thread_id:" << t2.get_id() << endl;
cout << "main thread_id:" << this_thread::get_id() << endl;
t2.join();
while (true) {
this_thread::sleep_for(chrono::milliseconds(1000));
break;
}
return 0;
}
输出:
t1_thread_id:thread func2
child thread_id:2
main thread_id:1