实例
#include<iostream>#include<cstdlib>#include<pthread.h>#include<unistd.h>usingnamespacestd; #defineNUM_THREADS 5void *wait(void *t){ inti; longtid; tid = (long)t; sleep(1); cout << "Sleeping in thread " << endl; cout << "Thread with id : " << tid << " ...exiting " << endl; pthread_exit(NULL);}intmain(){ intrc; inti; pthread_tthreads[NUM_THREADS]; pthread_attr_tattr; 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
Sleepingin thread
Threadwith id :4 ...exiting
Sleepingin thread
Threadwith id :3 ...exiting
Sleepingin thread
Threadwith id :2 ...exiting
Sleepingin thread
Threadwith id :1 ...exiting
Sleepingin thread
Threadwith 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.