#include<iostream>// 必須的頭文件#include<pthread.h>using namespace std;#define NUM_THREADS 5// 線程的運行函數void*say_hello(void* args){cout <<"Hello Runoob!"<< endl;return0;}intmain(){// 定義線程的 id 變量,多個變量使用數組pthread_t tids[NUM_THREADS];for(int i =0; i < NUM_THREADS;++i){//參數依次是:創建的線程id,線程參數,調用的函數,傳入的函數參數int ret =pthread_create(&tids[i],NULL, say_hello,NULL);if(ret !=0){cout <<"pthread_create error: error_code="<< ret << endl;}}//等各個線程退出后,進程才結束,否則進程強制結束了,線程可能還沒反應過來;pthread_exit(NULL);}
#include<iostream>#include<cstdlib>#include<pthread.h>using namespace std;#define NUM_THREADS 5struct thread_data{int thread_id;char*message;};void*PrintHello(void*threadarg){struct thread_data *my_data;my_data =(struct thread_data *) threadarg;cout <<"Thread ID : "<< my_data->thread_id ;cout <<" Message : "<< my_data->message << endl;pthread_exit(NULL);}int main (){pthread_t threads[NUM_THREADS];struct thread_data td[NUM_THREADS];int rc;int i;for( i=0; i < NUM_THREADS; i++){cout <<"main() : creating thread, "<< i << endl;td[i].thread_id = i;td[i].message =(char*)"This is message";rc =pthread_create(&threads[i],NULL,PrintHello,(void*)&td[i]);if(rc){cout <<"Error:unable to create thread,"<< rc << endl;exit(-1);}}pthread_exit(NULL);}
#include<iostream>#include<cstdlib>#include<pthread.h>#include<unistd.h>using namespace std;#define NUM_THREADS 5void*wait(void*t){int i;long tid;tid =(long)t;sleep(1);cout <<"Sleeping in thread "<< endl;cout <<"Thread with id : "<< tid <<" ...exiting "<< endl;pthread_exit(NULL);}int main (){int rc;int i;pthread_t threads[NUM_THREADS];pthread_attr_t attr;void*status;// 初始化并設置線程為可連接的(joinable)pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for( i=0; i < NUM_THREADS; i++){cout <<"main() : creating thread, "<< i << endl;rc =pthread_create(&threads[i],NULL, wait,(void*)&i );if(rc){cout <<"Error:unable to create thread,"<< rc << endl;exit(-1);}}// 刪除屬性,并等待其他線程pthread_attr_destroy(&attr);for( i=0; i < NUM_THREADS; i++){rc =pthread_join(threads[i],&status);if(rc){cout <<"Error:unable to join,"<< rc << endl;exit(-1);}cout <<"Main: completed thread id :"<< i ;cout <<" exiting with status :"<< status << endl;}cout <<"Main: program exiting."<< endl;pthread_exit(NULL);}
當上面的代碼被編譯和執行時,它會產生下列結果: main() : creating thread, 0 main() : creating thread, 1 main() : creating thread, 2 main() : creating thread, 3 main() : creating thread, 4 Sleeping in thread Thread with id : 4 …exiting Sleeping in thread Thread with id : 3 …exiting Sleeping in thread Thread with id : 2 …exiting Sleeping in thread Thread with id : 1 …exiting Sleeping in thread Thread with id : 0 …exiting Main: completed thread id :0 exiting with status :0 Main: completed thread id :1 exiting with status :0 Main: completed thread id :2 exiting with status :0 Main: completed thread id :3 exiting with status :0 Main: completed thread id :4 exiting with status :0 Main: program exiting.
#include<iostream>#include<thread>std::thread::id main_thread_id = std::this_thread::get_id();voidhello(){std::cout <<"Hello Concurrent World\n";if(main_thread_id == std::this_thread::get_id())std::cout <<"This is the main thread.\n";elsestd::cout <<"This is not the main thread.\n";}voidpause_thread(int n){std::this_thread::sleep_for(std::chrono::seconds(n));std::cout <<"pause of "<< n <<" seconds ended\n";}intmain(){std::thread t(hello);std::cout << t.hardware_concurrency()<< std::endl;//可以并發執行多少個(不準確)std::cout <<"native_handle "<< t.native_handle()<< std::endl;//可以并發執行多少個(不準確)t.join();std::thread a(hello);a.detach();std::thread threads[5];// 默認構造線程std::cout <<"Spawning 5 threads...\n";for(int i =0; i <5;++i)threads[i]= std::thread(pause_thread, i +1);// move-assign threadsstd::cout <<"Done spawning threads. Now waiting for them to join:\n";for(auto&thread : threads)thread.join();std::cout <<"All threads joined!\n";}