- 原始代码
#include <stdio.h>
#include <pthread.h>
void *Producer(void*);
void *Consumer(void*);
int main(){
pthread_t t_p;
pthread_t t_c;
pthread_create(&t_p,NULL,Producer,(void*)NULL);
pthread_create(&t_c,NULL,Consumer,(void*)NULL);
pthread_join(t_p,NULL);
pthread_join(t_c,NULL);
}
void *Producer(void *junk){
printf("生产者生产\n");
}
void *Consumer(void *junk){
printf("消费者消费\n");
}
执行结果:
消费者消费
生产者生产`
- 更改1
pthread_create(&t_p,NULL,Producer,(void*)NULL);
pthread_create(&t_c,NULL,Consumer,(void*)NULL);
//pthread_join(t_p,NULL);
pthread_join(t_c,NULL);
执行结果:
消费者消费
- 更改2
pthread_create(&t_p,NULL,Producer,(void*)NULL);
pthread_create(&t_c,NULL,Consumer,(void*)NULL);
pthread_join(t_p,NULL);
//pthread_join(t_c,NULL);
执行结果:
消费者消费
生产者生产
- 更改3
pthread_create(&t_c,NULL,Consumer,(void*)NULL);
pthread_create(&t_p,NULL,Producer,(void*)NULL);
pthread_join(t_p,NULL);
pthread_join(t_c,NULL);
执行结果:
生产者生产
消费者消费
- 更改4
pthread_create(&t_p,NULL,Producer,(void*)NULL);
pthread_create(&t_c,NULL,Consumer,(void*)NULL);
pthread_join(t_c,NULL);
pthread_join(t_p,NULL);
执行结果:
消费者消费
生产者生产
线程的顺序是随机的,当进行多次执行,或者换个电脑时,执行结果顺序改变!
版权声明:本文为qq_34991346原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。