zoukankan      html  css  js  c++  java
  • pthread_mutex_timedlock

    函数pthread_mutex_timedlock
    当线程试图获取一个已加锁的互斥变量时,pthread_mutex_timedlock互斥量原语允许绑定线程
    阻塞的时间。pthread_mutex_timedlock函数与pthread_mutex_lock是基本等价的,但是在达到超时
    时间值时,pthread_mutex_timedlock不会对互斥量进行加锁,而是返回错误码ETIMEDOUT。
    #include<pthread.h>
    #include<time.h>
    int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec 
    
    *restrict tsptr);
    超时指定愿意等待的绝对时间(与相对时间对比而言,指定在时间x之前可以阻塞等待,而不是说愿
    
    意阻塞Y秒)。这个超时时间是用timespec结构来表示的,它用秒和纳秒来描述时间。
    #include"apue.h"
    #include <pthread.h>
    #include<time.h>
    
    int
    main(void)
    {
        int err;
        struct timespec tout;
        struct tm *tmp;
        char buf[64];
        pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    
        pthread_mutex_lock(&lock);
        printf("mutex is locked
    ");
        clock_gettime(CLOCK_REALTIME, &tout);
        tmp = localtime(&tout.tv_sec);
        strftime(buf, sizeof(buf), "%r", tmp);
        printf("current time is %s
    ", buf);
        pthread_mutex_unlock(&lock);//这个是我自己加的,为了测试这个程序
        tout.tv_sec += 10;    /* 10 seconds from now */
        /* caution: this could lead to deadlock */
        err = pthread_mutex_timedlock(&lock, &tout);
        clock_gettime(CLOCK_REALTIME, &tout);
        tmp = localtime(&tout.tv_sec);
        strftime(buf, sizeof(buf), "%r", tmp);
        printf("the time is now %s
    ", buf);
        if (err == 0)
            printf("mutex locked again!
    ");
        else
            printf("can't lock mutex again: %s
    ", strerror(err));
        exit(0);
    }

  • 相关阅读:
    Gatling的进阶二
    scala环境搭建
    web性能测试的新利器
    Jmeter+jenkins接口性能测试平台实践整理(二)
    Gatling的进阶一
    [经验总结]利用xlstproc处理XSLT的makefile
    VBA在WORD中给表格外的字体设置为标题
    VBA赋值给指定单元格
    将压缩包隐藏到图片中
    DB2删除重复数据
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4595886.html
Copyright © 2011-2022 走看看