• 条件变量解决生产者消费者问题


    目录

    1.条件变量函数原型

    pthread_cond_init

    pthread_cond_destroy

    pthread_cond_wait

    pthread_cond_timedwait

    pthread_cond_signal

    pthread_cond_broadcast

    2.案例源码


    生产者消费者问题:生产者不能在容器满了继续生成,消费者不能在容器为空的时候消费

    1.条件变量函数原型

    条件变量的类型 pthread_cond_t

    pthread_cond_init

    int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);

    -功能:初始化

    pthread_cond_destroy

    int pthread_cond_destroy(pthread_cond_t *cond);

    -功能:回收资源

    pthread_cond_wait

     int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);

    -功能:等待,调用了该函数,线程会阻塞。当这个函数调用阻塞的时候,会对互斥锁进行解锁,当不阻塞的,继续向下执行,会重新加锁。

    pthread_cond_timedwait

    int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

    -功能:等待多少时间,调用了这个函数,线程会阻塞,知道指定的时间结束。

    pthread_cond_signal

    int pthread_cond_signal(pthread_cond_t *cond);

    -功能:唤醒一个或多个等待的线程

    pthread_cond_broadcast

    int pthread_cond_broadcast(pthread_cond_t *cond);

    -功能:唤醒所有的等待的线程

    2.案例源码

    通过设置条件变量来在没有资料的时候消费者等待生产者来生成

    1. #include
    2. #include
    3. #include
    4. #include
    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. newNode->next=head;
    21. head=newNode;
    22. newNode->num=rand()%1000;
    23. printf("add node, num %d,tid :%ld\n",newNode->num,pthread_self());
    24. //只要生成了一个,就通知消费者消费
    25. pthread_cond_signal(&cond);
    26. pthread_mutex_unlock(&mutex);
    27. usleep(100);
    28. }
    29. return NULL;
    30. }
    31. void *customer(void *arg){
    32. while(1){
    33. pthread_mutex_lock(&mutex);
    34. //保存头节点的指针
    35. struct Node* tmp=head;
    36. if(head!=NULL){
    37. head=head->next;
    38. printf("del node, num:%d,tid:%ld\n",tmp->num,pthread_self());
    39. free(tmp);
    40. pthread_mutex_unlock(&mutex);
    41. usleep(100);
    42. }else{
    43. pthread_cond_wait(&cond,&mutex);
    44. pthread_mutex_unlock(&mutex);
    45. }
    46. }
    47. return NULL;
    48. }
    49. int main(){
    50. pthread_mutex_init(&mutex,NULL);
    51. pthread_cond_init(&cond,NULL);
    52. //创建5个生产者线程和5个消费者线程
    53. pthread_t ptids[5],ctids[5];
    54. for(int i=0;i<5;i++){
    55. pthread_create(&ptids[i],NULL,producer,NULL);
    56. pthread_create(&ctids[i],NULL,customer,NULL);
    57. }
    58. for(int i=0;i<5;i++){
    59. pthread_detach(ptids[i]);
    60. pthread_detach(ptids[i]);
    61. }
    62. while(1){
    63. sleep(10);
    64. }
    65. pthread_mutex_destroy(&mutex);
    66. pthread_cond_destroy(&cond);
    67. pthread_exit(NULL);
    68. return 0;
    69. }

  • 相关阅读:
    13.TCP-bite
    Python入门自学进阶-Web框架——16、Django登录/注册
    计算机竞赛 机器视觉目标检测 - opencv 深度学习
    第三课:C++实现PDF去水印
    二分查找模版
    【数据库】E-R图相关知识、绘制方法及工具推荐
    GHOST工具访问数据库
    flask-admin菜鸟学习笔记
    负载均衡.
    机器学习之广义增量规则(Generalized Delta Rule)
  • 原文地址:https://blog.csdn.net/l_ethan/article/details/126017587