当前位置: 技术问答>linux和unix
请帮我看一下程序,有关多线程
来源: 互联网 发布时间:2015-10-07
本文导语: 两个线程,一个输入整数,另一个把这个数读出来。要求无数据时不能读,并且读写不能同时进行。谢谢 #include #include void mother(); void *producer(void *); void *consumer(void *); int i; int can_read = 0; pthread_mutex_t i_mutex=P...
两个线程,一个输入整数,另一个把这个数读出来。要求无数据时不能读,并且读写不能同时进行。谢谢
#include
#include
void mother();
void *producer(void *);
void *consumer(void *);
int i;
int can_read = 0;
pthread_mutex_t i_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;
int main(int argc,char **argv){
mother();
return 0;
}
void mother(){
pthread_t producer_thrd,consumer_thrd;
printf("mother %d:n",pthread_self());
int i=pthread_create(&producer_thrd,NULL,producer,NULL);//create thread
int j=pthread_create(&consumer_thrd,NULL,consumer,NULL);
pthread_mutex_destroy(&i_mutex);
pthread_cond_destroy(&got_request);
}
void * producer(void *vptr){
int j;
for(j=0;j
#include
#include
void mother();
void *producer(void *);
void *consumer(void *);
int i;
int can_read = 0;
pthread_mutex_t i_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t got_request = PTHREAD_COND_INITIALIZER;
int main(int argc,char **argv){
mother();
return 0;
}
void mother(){
pthread_t producer_thrd,consumer_thrd;
printf("mother %d:n",pthread_self());
int i=pthread_create(&producer_thrd,NULL,producer,NULL);//create thread
int j=pthread_create(&consumer_thrd,NULL,consumer,NULL);
pthread_mutex_destroy(&i_mutex);
pthread_cond_destroy(&got_request);
}
void * producer(void *vptr){
int j;
for(j=0;j