第一版:就单纯生产+消费,没有产品会while一直查看

    1. /*
    2. 生产者消费者模型:5个生成者,5个消费者,认为容器空间无限大
    3. 考虑:
    4. 1.如果容器中无数据,消费者无法取出,得等;
    5. 2.生成一个产品,就多一个,消费一个就少一个;
    6. */
    7. #include <stdio.h>
    8. #include <pthread.h>
    9. #include <stdlib.h>
    10. #include <unistd.h>
    11. // 创建一个互斥量
    12. pthread_mutex_t mutex;
    13. struct Node{
    14. int num;
    15. struct Node *next;
    16. };
    17. // 头结点
    18. struct Node * head = NULL;
    19. void * producer(void * arg) {
    20. // 不断的创建新的节点,添加到链表中
    21. while(1) {
    22. pthread_mutex_lock(&mutex);
    23. struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
    24. newNode->next = head;
    25. head = newNode;
    26. newNode->num = rand() % 900 + 100;
    27. printf("生成产品num : %d, tid : %ld\n", newNode->num, pthread_self());
    28. pthread_mutex_unlock(&mutex);
    29. usleep(100);
    30. }
    31. return NULL;
    32. }
    33. void * customer(void * arg) {
    34. while(1) { // 非阻塞,一直while等待
    35. pthread_mutex_lock(&mutex);
    36. // 保存头结点的指针
    37. struct Node * tmp = head;
    38. // 判断是否有数据
    39. if(head != NULL) {
    40. // 有数据
    41. head = head->next;
    42. printf("消费产品num : %d, tid : %ld\n", tmp->num, pthread_self());
    43. free(tmp);
    44. pthread_mutex_unlock(&mutex);
    45. usleep(100);
    46. } else {
    47. // 没有数据
    48. pthread_mutex_unlock(&mutex);
    49. }
    50. }
    51. return NULL;
    52. }
    53. int main() {
    54. pthread_mutex_init(&mutex, NULL);
    55. // 创建5个生产者线程,和5个消费者线程
    56. pthread_t ptids[5], ctids[5];
    57. for(int i = 0; i < 5; i++) {
    58. pthread_create(&ptids[i], NULL, producer, NULL);
    59. pthread_create(&ctids[i], NULL, customer, NULL);
    60. }
    61. for(int i = 0; i < 5; i++) {
    62. pthread_detach(ptids[i]);
    63. pthread_detach(ctids[i]);
    64. }
    65. while(1) {
    66. sleep(10);
    67. }
    68. pthread_mutex_destroy(&mutex);
    69. pthread_exit(NULL);
    70. return 0;
    71. }

    第二版:利用条件变量,实现当容器没有产品后消费者通知生产者去生产,生产者生产后又通知消费者的功能

    1. #include <stdio.h>
    2. #include <pthread.h>
    3. #include <stdlib.h>
    4. #include <unistd.h>
    5. // 创建一个互斥量
    6. pthread_mutex_t mutex;
    7. // 创建条件变量
    8. pthread_cond_t cond;
    9. struct Node{
    10. int num;
    11. struct Node *next;
    12. };
    13. // 头结点
    14. struct Node * head = NULL;
    15. void * producer(void * arg) {
    16. // 不断的创建新的节点,添加到链表中
    17. while(1) {
    18. pthread_mutex_lock(&mutex);
    19. struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
    20. if (head == NULL) {
    21. newNode->next = head;
    22. head = newNode;
    23. newNode->num = rand() % 900 + 100;
    24. printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
    25. printf("已经生产了,通知消费者,可以来消费了~\n");
    26. }else {
    27. newNode->next = head;
    28. head = newNode;
    29. newNode->num = rand() % 900 + 100;
    30. printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
    31. }
    32. // 只要生产了一个,就通知消费者消费
    33. pthread_cond_signal(&cond);
    34. pthread_mutex_unlock(&mutex);
    35. usleep(120);
    36. }
    37. return NULL;
    38. }
    39. void * customer(void * arg) {
    40. while(1) {
    41. pthread_mutex_lock(&mutex);
    42. // 保存头结点的指针
    43. struct Node * tmp = head;
    44. // 判断是否有数据
    45. if(head != NULL) {
    46. // 有数据
    47. head = head->next;
    48. printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
    49. free(tmp);
    50. if(head == NULL) printf("没有数据了,通知生产者,赶紧生产!\n");
    51. pthread_mutex_unlock(&mutex);
    52. usleep(100);
    53. } else {
    54. // 没有数据,需要等待
    55. // 当这个函数调用阻塞的时候,会对互斥锁进行解锁,当不阻塞的,继续向下执行,会重新加锁。
    56. pthread_cond_wait(&cond, &mutex);
    57. pthread_mutex_unlock(&mutex);
    58. }
    59. }
    60. return NULL;
    61. }
    62. int main() {
    63. pthread_mutex_init(&mutex, NULL);
    64. pthread_cond_init(&cond, NULL);
    65. // 创建5个生产者线程,和5个消费者线程
    66. pthread_t ptids[5], ctids[5];
    67. for(int i = 0; i < 5; i++) {
    68. pthread_create(&ptids[i], NULL, producer, NULL);
    69. pthread_create(&ctids[i], NULL, customer, NULL);
    70. }
    71. for(int i = 0; i < 5; i++) {
    72. pthread_detach(ptids[i]);
    73. pthread_detach(ctids[i]);
    74. }
    75. while(1) {
    76. sleep(10);
    77. }
    78. pthread_mutex_destroy(&mutex);
    79. pthread_cond_destroy(&cond);
    80. pthread_exit(NULL);
    81. return 0;
    82. }

    第三版:利用信号量,来判断容器中有无产品(是否装满),从而决定某线程是阻塞还是执行,可以设置容器大小

    1. #include <stdio.h>
    2. #include <pthread.h>
    3. #include <stdlib.h>
    4. #include <unistd.h>
    5. #include <semaphore.h>
    6. // 创建一个互斥量
    7. pthread_mutex_t mutex;
    8. // 创建两个信号量
    9. sem_t psem;
    10. sem_t csem;
    11. struct Node{
    12. int num;
    13. struct Node *next;
    14. };
    15. // 头结点
    16. struct Node * head = NULL;
    17. void * producer(void * arg) {
    18. // 不断的创建新的节点,添加到链表中
    19. while(1) {
    20. sem_wait(&psem); // 容器空间-1
    21. pthread_mutex_lock(&mutex);
    22. struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
    23. newNode->next = head;
    24. head = newNode;
    25. newNode->num = rand() % 900 + 100;
    26. printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
    27. pthread_mutex_unlock(&mutex);
    28. sem_post(&csem); // 告诉消费者有多少个产品可以消费
    29. }
    30. return NULL;
    31. }
    32. void * customer(void * arg) {
    33. while(1) {
    34. sem_wait(&csem); // 可消费产品-1
    35. pthread_mutex_lock(&mutex);
    36. // 保存头结点的指针
    37. struct Node * tmp = head;
    38. head = head->next;
    39. printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
    40. free(tmp);
    41. pthread_mutex_unlock(&mutex);
    42. sem_post(&psem); // 容器空间+1
    43. }
    44. return NULL;
    45. }
    46. int main() {
    47. pthread_mutex_init(&mutex, NULL);
    48. sem_init(&psem, 0, 8); // 表示容器最大容纳8个产品
    49. sem_init(&csem, 0, 0);
    50. // 创建5个生产者线程,和5个消费者线程
    51. pthread_t ptids[5], ctids[5];
    52. for(int i = 0; i < 5; i++) {
    53. pthread_create(&ptids[i], NULL, producer, NULL);
    54. pthread_create(&ctids[i], NULL, customer, NULL);
    55. }
    56. for(int i = 0; i < 5; i++) {
    57. pthread_detach(ptids[i]);
    58. pthread_detach(ctids[i]);
    59. }
    60. while(1) {
    61. sleep(10);
    62. }
    63. pthread_mutex_destroy(&mutex);
    64. pthread_exit(NULL);
    65. return 0;
    66. }