第一版:就单纯生产+消费,没有产品会while一直查看
/*生产者消费者模型:5个生成者,5个消费者,认为容器空间无限大考虑:1.如果容器中无数据,消费者无法取出,得等;2.生成一个产品,就多一个,消费一个就少一个;*/#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>// 创建一个互斥量pthread_mutex_t mutex;struct Node{int num;struct Node *next;};// 头结点struct Node * head = NULL;void * producer(void * arg) {// 不断的创建新的节点,添加到链表中while(1) {pthread_mutex_lock(&mutex);struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));newNode->next = head;head = newNode;newNode->num = rand() % 900 + 100;printf("生成产品num : %d, tid : %ld\n", newNode->num, pthread_self());pthread_mutex_unlock(&mutex);usleep(100);}return NULL;}void * customer(void * arg) {while(1) { // 非阻塞,一直while等待pthread_mutex_lock(&mutex);// 保存头结点的指针struct Node * tmp = head;// 判断是否有数据if(head != NULL) {// 有数据head = head->next;printf("消费产品num : %d, tid : %ld\n", tmp->num, pthread_self());free(tmp);pthread_mutex_unlock(&mutex);usleep(100);} else {// 没有数据pthread_mutex_unlock(&mutex);}}return NULL;}int main() {pthread_mutex_init(&mutex, NULL);// 创建5个生产者线程,和5个消费者线程pthread_t ptids[5], ctids[5];for(int i = 0; i < 5; i++) {pthread_create(&ptids[i], NULL, producer, NULL);pthread_create(&ctids[i], NULL, customer, NULL);}for(int i = 0; i < 5; i++) {pthread_detach(ptids[i]);pthread_detach(ctids[i]);}while(1) {sleep(10);}pthread_mutex_destroy(&mutex);pthread_exit(NULL);return 0;}
第二版:利用条件变量,实现当容器没有产品后消费者通知生产者去生产,生产者生产后又通知消费者的功能
#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>// 创建一个互斥量pthread_mutex_t mutex;// 创建条件变量pthread_cond_t cond;struct Node{int num;struct Node *next;};// 头结点struct Node * head = NULL;void * producer(void * arg) {// 不断的创建新的节点,添加到链表中while(1) {pthread_mutex_lock(&mutex);struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));if (head == NULL) {newNode->next = head;head = newNode;newNode->num = rand() % 900 + 100;printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());printf("已经生产了,通知消费者,可以来消费了~\n");}else {newNode->next = head;head = newNode;newNode->num = rand() % 900 + 100;printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());}// 只要生产了一个,就通知消费者消费pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);usleep(120);}return NULL;}void * customer(void * arg) {while(1) {pthread_mutex_lock(&mutex);// 保存头结点的指针struct Node * tmp = head;// 判断是否有数据if(head != NULL) {// 有数据head = head->next;printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());free(tmp);if(head == NULL) printf("没有数据了,通知生产者,赶紧生产!\n");pthread_mutex_unlock(&mutex);usleep(100);} else {// 没有数据,需要等待// 当这个函数调用阻塞的时候,会对互斥锁进行解锁,当不阻塞的,继续向下执行,会重新加锁。pthread_cond_wait(&cond, &mutex);pthread_mutex_unlock(&mutex);}}return NULL;}int main() {pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);// 创建5个生产者线程,和5个消费者线程pthread_t ptids[5], ctids[5];for(int i = 0; i < 5; i++) {pthread_create(&ptids[i], NULL, producer, NULL);pthread_create(&ctids[i], NULL, customer, NULL);}for(int i = 0; i < 5; i++) {pthread_detach(ptids[i]);pthread_detach(ctids[i]);}while(1) {sleep(10);}pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);pthread_exit(NULL);return 0;}
第三版:利用信号量,来判断容器中有无产品(是否装满),从而决定某线程是阻塞还是执行,可以设置容器大小
#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>#include <semaphore.h>// 创建一个互斥量pthread_mutex_t mutex;// 创建两个信号量sem_t psem;sem_t csem;struct Node{int num;struct Node *next;};// 头结点struct Node * head = NULL;void * producer(void * arg) {// 不断的创建新的节点,添加到链表中while(1) {sem_wait(&psem); // 容器空间-1pthread_mutex_lock(&mutex);struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));newNode->next = head;head = newNode;newNode->num = rand() % 900 + 100;printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());pthread_mutex_unlock(&mutex);sem_post(&csem); // 告诉消费者有多少个产品可以消费}return NULL;}void * customer(void * arg) {while(1) {sem_wait(&csem); // 可消费产品-1pthread_mutex_lock(&mutex);// 保存头结点的指针struct Node * tmp = head;head = head->next;printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());free(tmp);pthread_mutex_unlock(&mutex);sem_post(&psem); // 容器空间+1}return NULL;}int main() {pthread_mutex_init(&mutex, NULL);sem_init(&psem, 0, 8); // 表示容器最大容纳8个产品sem_init(&csem, 0, 0);// 创建5个生产者线程,和5个消费者线程pthread_t ptids[5], ctids[5];for(int i = 0; i < 5; i++) {pthread_create(&ptids[i], NULL, producer, NULL);pthread_create(&ctids[i], NULL, customer, NULL);}for(int i = 0; i < 5; i++) {pthread_detach(ptids[i]);pthread_detach(ctids[i]);}while(1) {sleep(10);}pthread_mutex_destroy(&mutex);pthread_exit(NULL);return 0;}
