当前位置: 首页 > news >正文

品牌网站建设-建站之路成免费crm软件有哪些优点

品牌网站建设-建站之路,成免费crm软件有哪些优点,怎么做淘宝网站教程,怎么在自己做的网站上发视频教程pthread 条件变量使用注意 使用 pthread_cond_timedwait 等待条件变量时,其默认使用的为系统时间,若在其等待期间修改系统时间,则会导致其提前结束。 测试步骤 运行以下代码。 使用 date 命令查看系统时间,假设输出为 Thu Jan …

pthread 条件变量使用注意

  • 使用 pthread_cond_timedwait 等待条件变量时,其默认使用的为系统时间,若在其等待期间修改系统时间,则会导致其提前结束。

测试步骤

  • 运行以下代码。

  • 使用 date 命令查看系统时间,假设输出为 Thu Jan 1 08:01:53 AM CST 1970

  • 使用 date -s 08:03:53 设置系统时间,程序会立刻退出,并打印 wait timed out

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <errno.h>
    #include <unistd.h>pthread_cond_t g_cond;
    pthread_mutex_t g_mutex;void* thread1_func(void* arg)
    {// 必须先获取互斥锁pthread_mutex_lock(&g_mutex);// 获取当前时间struct timespec ts;clock_gettime(CLOCK_REALTIME, &ts);// 设置等待时间为 100 秒ts.tv_sec += 100;// 等待条件变量被唤醒,或者等待时间超时printf("waiting cond signal\n");int ret = pthread_cond_timedwait(&g_cond, &g_mutex, &ts);if (ret == 0) {printf("get cond signaled\n");} else if (ret == ETIMEDOUT) {printf("wait timed out\n");} else {printf("wait failed\n");}// 解锁pthread_mutex_unlock(&g_mutex);return NULL;
    }int main()
    {pthread_cond_init(&g_cond, NULL);pthread_mutex_init(&g_mutex, NULL);// 创建线程pthread_t th1;pthread_create(&th1, NULL, thread1_func, NULL);// 等待线程结束pthread_join(th1, NULL);pthread_mutex_destroy(&g_mutex);pthread_cond_destroy(&g_cond);return 0;
    }
    

解决方案

  • 初始化条件变量时,设置其使用稳定时钟即可。

  • 示例代码如下:

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <errno.h>
    #include <unistd.h>pthread_cond_t g_cond;
    pthread_mutex_t g_mutex;void* thread1_func(void* arg)
    {// 必须先获取互斥锁pthread_mutex_lock(&g_mutex);// 获取当前时间struct timespec ts;// 此处与上文不同clock_gettime(CLOCK_MONOTONIC, &ts);// 设置等待时间为 100 秒ts.tv_sec += 100;// 等待条件变量被唤醒,或者等待时间超时printf("waiting cond signal\n");int ret = pthread_cond_timedwait(&g_cond, &g_mutex, &ts);if (ret == 0) {printf("get cond signaled\n");} else if (ret == ETIMEDOUT) {printf("wait timed out\n");} else {printf("wait failed\n");}// 解锁pthread_mutex_unlock(&g_mutex);return NULL;
    }int main()
    {pthread_condattr_t condattr;pthread_condattr_init(&condattr);// 设置使用稳定时钟pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);pthread_cond_init(&g_cond, &condattr);pthread_mutex_init(&g_mutex, NULL);// 创建线程pthread_t th1;pthread_create(&th1, NULL, thread1_func, NULL);// 等待线程结束pthread_join(th1, NULL);pthread_mutex_destroy(&g_mutex);pthread_cond_destroy(&g_cond);return 0;
    }
    
http://www.hkea.cn/news/562763/

相关文章:

  • 上海网站建设公司排名西安百度公司
  • 中国网网址是多少网站推广优化教程
  • 关于加强机关网站建设运营培训
  • dw做的网站怎么让别人看到如何建立一个网站
  • 保险网站建设优缺点seo代码优化步骤
  • 如何快速建网站百度电脑版入口
  • 山东省建设工程信息网站最近最新的新闻
  • 免费网站建设方案锦绣大地seo官网
  • 电子商务的网站建设牛排seo系统
  • 资源收费网站怎么做网站快速优化排名官网
  • 招标网哪个网站信息可靠百度站长工具网站
  • 郑州七七网站建设互联网推广公司
  • 佛山做外贸网站代理商百度收录技术
  • 公司网站建设需要什么今日热搜第一名
  • 烟台建设企业网站网站快速收录入口
  • 怎么做繁体字网站网络营销公司注册找哪家
  • 做ppt介绍网站吗网站搜索引擎优化工具
  • 深圳网站建设有没有市场百度搜索推广的五大优势
  • 网站建设好的图片百度互联网营销
  • 柳州网站制作公司seo优化什么意思
  • 网站建设做的好的公司淘宝关键词优化怎么弄
  • 手机网站用模版方象科技的企业愿景
  • 沈阳网站建设技术公司排名公司市场营销策划方案
  • 赣州网站建设怎样石家庄最新消息
  • 公司注册地址和经营地址不一致可以吗长春seo招聘
  • 好的做问卷调查的网站好推广有奖励的app平台
  • 有专业设计网站吗百度指数与百度搜索量
  • 网站的整体结构百度云网盘资源搜索引擎入口
  • 咸阳网站建设哪家专业杭州优化公司在线留言
  • 地板网站建设门户网站