• 线程2——条件变量


    动态创建条件变量

    int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);


    注销一个条件变量

    int pthread_cond_destroy(pthread_cond_t *cond);


    无条件等待

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


    计时等待

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


    发送单个条件变量成立信号

    int pthread_cond_signal(pthread_cond_t *cond)


     广播条件变量成立信号

    int pthread_cond_ broadcast (pthread_cond_t *cond)


     1 #include <pthread.h>
     2 #include <stdio.h>
     3 #include <unistd.h>
     4 #include <stdlib.h>
     5 #include <sys/time.h>
     6 //声明两个线程
     7 void* task1(void *arg);
     8 void* task2(void *arg);
     9 pthread_t tid[3];                    //线程的3个指针
    10 pthread_mutex_t lock;                //互斥锁指针
    11 pthread_cond_t cond;                //条件变量指针
    12 int main(void)
    13 {
    14     int ret;
    15     
    16     //初始化互斥锁和条件变量
    17     pthread_mutex_init(&lock,NULL);
    18     pthread_cond_init(&cond,NULL);
    19     //创建2个线程
    20     pthread_create(&tid[0],NULL,task1,NULL);
    21     pthread_create(&tid[1],NULL,task2,NULL);
    22     //等待
    23     pthread_join(tid[0],NULL);
    24     pthread_join(tid[1],NULL);
    25 
    26     printf("Main:ALL new threads end
    ");
    27     //销毁互斥锁
    28     pthread_mutex_destroy(&lock);
    29     pthread_exit(NULL);
    30 }
    31 
    32 void* task1(void *arg)
    33 {
    34     struct timeval now;
    35     /*
    36      struct timeval 
    37         {
    38           time_t       tv_sec;    //  seconds 秒
    39           suseconds_t   tv_usec; // microseconds 毫秒
    40         };
    41     */
    42     struct timespec outtime;
    43     /*
    44     struct timespec 
    45         {
    46             time_t tv_sec; // seconds 秒
    47             long tv_nsec; // and nanoseconds 纳秒 
    48         };
    49     */
    50     printf("I am task1
    ");
    51     pthread_mutex_lock(&lock);        //上锁
    52     printf("task1: wait max time is 5s
    ");
    53     gettimeofday(&now, NULL);        //返回自1970-01-01 00:00:00到现在经历的秒数
    54     outtime.tv_sec = now.tv_sec + 5; 
    55     outtime.tv_nsec = now.tv_usec * 1000;
    56     pthread_cond_timedwait(&cond, &lock, &outtime);//线程1等待条件变量,最多等待5s
    57     printf("task1: coutinue run
    ");
    58     printf("task1: ready to pthread_exit
    ");
    59     
    60     pthread_exit(NULL);
    61 }
    62 
    63 
    64 void* task2(void *arg)
    65 {
    66     printf("I am task2
    ");
    67     sleep(3);      //线程2休眠3s后激活条件变量
    68     printf("task2: ready to signal the cond
    ");
    69     pthread_cond_signal(&cond);    //发送变量成立信号 用来激活条件变量
    70     printf("task2: ready to pthread_exit
    ");
    71     pthread_exit(NULL);
    72 }

    运行后代码如下

    1 k1: wait max time is 5s
    2 I am task2
    3 task2: ready to signal the cond
    4 task2: ready to pthread_exit
    5 task1: coutinue run
    6 task1: ready to pthread_exit
    7 Main:ALL new threads end

    先运行task1,触发条件变量,跳到task2激活,执行task2的两句话,然后再回到task1执行两句话,最后回到主函数

    计时等待:pthread_cond_timedwait();用法:

    1 struct timeval now;
    2 struct timespec outtime;
    3 gettimeofday(&now, NULL);//获取当前时间
    4 outtime.tv_sec = now.tv_sec + 5; //设定唤醒的绝对时间是当前时间+5s
    5 outtime.tv_nsec = now.tv_usec * 1000;
    6 pthread_cond_timedwait(&cond, &lock, &outtime);

    用到的两个结构体

     1 struct timeval{  
     2     long int tv_sec; // 秒数  
     3     long int tv_usec; // 微秒数  
     4 }  
     5 
     6 
     7 
     8 struct timespec {
     9     time_t tv_sec; // seconds 秒 
    10     long tv_nsec; // and nanoseconds 毫秒 
    11 };
  • 相关阅读:
    linux下实现nginx反向代理
    linux下实现nginx安装实现端口区分,域名区分
    redis整合spring
    linux下安装redis以及redis集群
    Kindeditor编辑器
    ActiveMQ
    redis(保存邮件激活码)
    AngularJS (验证码倒计时)
    linux部署项目(oracle+redis+activeMQ+elasticSearch+tomcat+JDK8)
    WebService
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/11673943.html
Copyright © 2020-2023  润新知