找回密码
 立即注册
首页 资源区 代码 条件量练习

条件量练习

任静柔 2025-5-29 10:51:53
  1. /*********************************************************************************
  2. *
  3. * 设计程序,创建三个线程,任务1条件满足时解除任务二的挂起状态,任务3用于控制x的值
  4. * author:jindouliu2024@163.com
  5. * date:2025.5.12
  6. *
  7. *
  8. * Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
  9. ********************************************************************************/
复制代码
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. pthread_t task1_id;
  5. pthread_t task2_id;
  6. pthread_t task3_id;
  7. pthread_t main_id;
  8. int x = 10,y = 20;
  9. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  10. pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;
  11. void * task1(void *arg)
  12. {
  13.        
  14.         while(1){
  15.                 //上锁
  16.                  pthread_mutex_lock(&mutex);
  17.                 //如果不满足,则一直阻塞等待
  18.                 while(x <= y){
  19.                         printf("running1\n");
  20.                          pthread_cond_wait(&cond,&mutex);
  21.                 }
  22.                 //如果满足,则通知唤醒
  23.                 pthread_cond_broadcast(&cond);
  24.                 //解锁
  25.                 pthread_mutex_unlock(&mutex);
  26.                
  27.         }
  28.         pthread_exit(NULL);
  29. }
  30. void * task2(void *arg)
  31. {
  32.        
  33.         while(1){
  34.                 //上锁
  35.                  pthread_mutex_lock(&mutex);
  36.                  printf("running2\n");
  37.                 //如果不满足,则一直阻塞等待
  38.                 if(x > y){
  39.                         printf("条件满足\n");
  40.                          pthread_cond_broadcast(&cond);
  41.                 }
  42.                 //解锁
  43.                 pthread_mutex_unlock(&mutex);
  44.                 sleep(1);
  45.         }
  46.         pthread_exit(NULL);
  47. }
  48. void * task3(void *arg)
  49. {
  50.        
  51.         while(1){
  52.                 //上锁
  53.                 pthread_mutex_lock(&mutex);
  54.                 if(x > y){
  55.                         y += 10;
  56.                 }
  57.                 x += 2;
  58.                 printf("running3\n");
  59.                
  60.                 //解锁
  61.                 pthread_mutex_unlock(&mutex);
  62.                 sleep(1);
  63.         }
  64.         pthread_exit(NULL);
  65. }
  66. int main()
  67. {
  68.         //初始化条件变量
  69.         pthread_cond_init(&cond,NULL);
  70.         //初始化互斥锁
  71.         pthread_mutex_init(&mutex,NULL);
  72.         //设置主线程为可分离属性
  73.         pthread_detach(pthread_self());
  74.         //pthread_t task1_id,task2_id,main_id;
  75.         //定义线程属性对象
  76.         pthread_attr_t thread_arr;
  77.         //初始化线程属性对象
  78.         pthread_attr_init(&thread_arr);
  79.         //设置线程属性
  80.         pthread_attr_setdetachstate(&thread_arr,PTHREAD_CREATE_DETACHED);
  81.         //使用线程属性创建线程
  82.         pthread_create(&task1_id,&thread_arr,task1,NULL);
  83.         pthread_create(&task2_id,&thread_arr,task2,NULL);
  84.         pthread_create(&task3_id,&thread_arr,task3,NULL);
  85.         //主线程退出
  86.         pthread_exit(NULL);
  87.         return 0;
  88. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册