###### tags: `作業系統` # *2022/10/11 作業_Version02* ## 題目: Please write a program that has multiple threads,which two or more threads run at the same time. ## 程式碼 ``` #include <bits/stdc++.h> using namespace std; void thread_1(void); void thread_2(void); int main(int argc, const char * argv[]) { thread t1(thread_1);//Build thread 1 thread t2(thread_2);//Build thread 2 std::this_thread::sleep_for(std::chrono::seconds(1)); cout<<"Main function, thread_1 and thread_2 finished!"<<endl; cout<<"Thread_1 end"<<endl; t1.join();//Wait for the output of thread_1 cout<<"Thread_2 end"<<endl; t2.join();//Wait for the output of thread_2 return 0; } void thread_1(void){ cout<<"Hello"<<endl; cout<<"This is thread_1"<<endl; } void thread_2(void){ cout<<"Hello"<<endl; cout<<"This is thread_2"<<endl; } ``` ## 輸出結果 ``` Hello This is thread_1 Hello This is thread_2 Main function, thread_1 and thread_2 finished! Thread_1 end Thread_2 end Program ended with exit code: 0 ```