程序功能:
主线程向list<int>添加数据,两个子线程冲list<int>中取数据并删除该数据。主线程类似于Service线程,子线程类是于work线程。
Code
#include<iostream> #include<pthread.h> #include<list> #include<unistd.h> #include<stdlib.h> #include<time.h> using namespace std; pthread_mutex_t vc_mutex; list<int>* pgusts = NULL; int gust_max_indx = 0; int get_rand_num() { #define MAX_SLEEP_TIME 30 time_t tsr; srand(time(&tsr)); int rnum = rand()%MAX_SLEEP_TIME; return rnum; } void * handle_hair(void *arg) { pthread_t pth_id = pthread_self(); cout<<"child pid<handle_hair>:\t"<<(unsigned int)pth_id<<endl; while(1) { pthread_mutex_lock(&vc_mutex); if(NULL != pgusts && !pgusts->empty()) { int igust = pgusts->front(); pgusts->pop_front(); cout<<"pthread "<<(unsigned int)pth_id<<" handle"<<igust<<endl; } pthread_mutex_unlock(&vc_mutex); sleep(get_rand_num()); } } void * add_gust(void *arg) { pthread_t pth_id = pthread_self(); while(1) { sleep(get_rand_num()+10); int gust_num = get_rand_num()+1; int i = 0; while(i<gust_num) { pgusts->push_back(i+gust_max_indx+1); ++i; } gust_max_indx+=gust_num; } } int main(void) { int err; if(0 != pthread_mutex_init(&vc_mutex,NULL)) { cerr<<"[error]initialize mutex fail"<<endl; return 1; } pgusts = new list<int>; if ( NULL == pgusts ) { cerr<<"[error]malloc list<int> fail"<<endl; return 1; } pthread_t pth_id = pthread_self(); cout<<"main pid:\t"<<(unsigned int)pth_id<<endl; err = pthread_create(&pth_id,NULL,handle_hair,NULL); if(0 != err ) { cerr<<"[error]pthread_create fail"<<endl; return 1; } cout<<"child pid:\t"<<(unsigned int)pth_id<<endl; err = pthread_create(&pth_id,NULL,handle_hair,NULL); if(0 != err ) { cerr<<"[error]pthread_create fail"<<endl; return 1; } cout<<"child pid:\t"<<(unsigned int)pth_id<<endl; add_gust(NULL); return 0; }
编译Code:
g++ -g -Wall -o barber_out barber.c -lpthread
注意这里必须要添加-lpthread选项,因为pthread不是linux活在标准的c/c++库
运行结果:
main pid: 267999040 child pid: 251287296 child pid: 242894592 child pid<handle_hair>: 251287296 child pid<handle_hair>: 242894592 pthread 251287296 handle1 pthread 242894592 handle2 pthread 251287296 handle3 pthread 242894592 handle4 pthread 251287296 handle5 pthread 242894592 handle6 pthread 251287296 handle7 pthread 242894592 handle8 pthread 251287296 handle9 pthread 242894592 handle10 pthread 251287296 handle11 pthread 242894592 handle12 pthread 251287296 handle13 pthread 242894592 handle14 pthread 242894592 handle15 pthread 251287296 handle16 pthread 251287296 handle17 pthread 251287296 handle18 pthread 242894592 handle19 pthread 242894592 handle20 pthread 242894592 handle21 pthread 242894592 handle22 pthread 242894592 handle23 pthread 242894592 handle24 pthread 242894592 handle25 pthread 242894592 handle26 pthread 242894592 handle27 pthread 242894592 handle28 pthread 242894592 handle29 pthread 242894592 handle30 pthread 242894592 handle31 pthread 242894592 handle32 pthread 242894592 handle33 pthread 242894592 handle34 pthread 242894592 handle35 pthread 242894592 handle36 pthread 242894592 handle37 pthread 242894592 handle38 pthread 242894592 handle39 pthread 242894592 handle40 pthread 242894592 handle41 pthread 242894592 handle42 pthread 242894592 handle43 pthread 242894592 handle44 pthread 242894592 handle45 pthread 242894592 handle46 pthread 242894592 handle47 pthread 242894592 handle48 pthread 242894592 handle49 pthread 242894592 handle50 pthread 242894592 handle51 pthread 242894592 handle52 pthread 242894592 handle53 pthread 242894592 handle54 pthread 242894592 handle55 pthread 242894592 handle56 pthread 242894592 handle57 pthread 242894592 handle58 pthread 242894592 handle59 pthread 242894592 handle60 pthread 242894592 handle61 pthread 242894592 handle62 pthread 242894592 handle63 pthread 242894592 handle64 pthread 242894592 handle65 pthread 242894592 handle66 pthread 242894592 handle67 pthread 242894592 handle68 pthread 242894592 handle69 pthread 242894592 handle70 pthread 251287296 handle71 pthread 242894592 handle72 pthread 251287296 handle73 pthread 242894592 handle74 pthread 251287296 handle75 pthread 242894592 handle76 pthread 251287296 handle77 pthread 242894592 handle78 pthread 251287296 handle79 pthread 251287296 handle80 pthread 251287296 handle81 pthread 251287296 handle82 pthread 251287296 handle83 pthread 251287296 handle84 pthread 251287296 handle85 pthread 251287296 handle86 pthread 251287296 handle87 pthread 251287296 handle88 pthread 251287296 handle89 pthread 251287296 handle90 pthread 251287296 handle91 pthread 251287296 handle92 pthread 251287296 handle93 pthread 251287296 handle94 pthread 251287296 handle95 pthread 251287296 handle96 pthread 251287296 handle97 pthread 251287296 handle98 pthread 251287296 handle99 pthread 251287296 handle100 pthread 251287296 handle101 pthread 251287296 handle102 pthread 251287296 handle103 pthread 251287296 handle104 pthread 251287296 handle105 pthread 251287296 handle106 pthread 251287296 handle107 pthread 251287296 handle108 pthread 251287296 handle109 pthread 251287296 handle110 pthread 251287296 handle111 pthread 251287296 handle112 pthread 251287296 handle113 pthread 251287296 handle114 pthread 251287296 handle115 pthread 251287296 handle116 pthread 251287296 handle117 pthread 251287296 handle118 pthread 251287296 handle119 pthread 251287296 handle120 pthread 251287296 handle121 pthread 251287296 handle122 pthread 251287296 handle123 pthread 242894592 handle124 pthread 251287296 handle125 pthread 242894592 handle126 pthread 251287296 handle127
查看线程运行情况:
ialan@Apprentice>thread$ top -c -H -p 3901
top - 18:01:02 up 2:06, 3 users, load average: 0.74, 0.68, 0.73 top - 18:01:15 up 2:06, 3 users, load average: 0.86, 0.72, 0.74 Tasks: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombie Cpu(s): 22.0%us, 4.3%sy, 0.0%ni, 72.1%id, 0.8%wa, 0.0%hi, 0.7%si, 0.0%st Mem: 3903276k total, 2261200k used, 1642076k free, 64988k buffers Swap: 7999484k total, 0k used, 7999484k free, 878068k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3901 ialan 20 0 31124 1104 928 S 0 0.0 0:00.00 ./barber_out 3902 ialan 20 0 31124 1104 928 S 0 0.0 0:00.00 ./barber_out 3903 ialan 20 0 31124 1104 928 S 0 0.0 0:00.00 ./barber_out