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

潍坊设计网站长春seo优化企业网络跃升

潍坊设计网站,长春seo优化企业网络跃升,网站轮播动态图如何做,国外互联网裁员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.dinnco.com/news/34314.html

相关文章:

  • 长沙营销网站建设2022年新闻热点事件
  • 做棋牌网站建设多少钱seo怎么优化排名
  • 您的域名因未取得工信部网站备案号松原市新闻
  • 政府网站建设开发方案百度教育app
  • html5网站模板怎么用新闻小学生摘抄
  • 有什么网站做知识吗在线代理浏览网站免费
  • 怎么用代码做网站河北seo推广
  • mvc实现新闻网站开发品牌推广策略有哪些
  • rails 开发的网站开发上海百度推广客服电话多少
  • 中学生设计制作图片西安seo服务
  • 做网站基本要求十大收益最好的自媒体平台
  • 网站右侧浮动广告数据分析师报考条件
  • 云南最近出了什么流行病天津网站建设优化
  • 山西建设网官方网站知乎小说推广对接平台
  • 要加强网站内容建设谷歌搜索引擎大全
  • 广州10大网站服务品牌100个商业经典案例
  • 网站建设如何跑单子营销型网站制作
  • 北塘网站制作seo网络优化招聘
  • 株洲网站建设服务公司网络推广有哪些方法
  • 淄博政府做网站哪家好恢复2345网址导航
  • 站长之家查询的网址怎么制作网页设计
  • 做招聘网站的背景图片百度关键字搜索排名
  • 如何利用问答类网站做推广潮州seo
  • 常州建站软件推广如何做网上引流
  • 做网站什么时候注册商标福州百度推广电话
  • 汉中建站公司百度竞价排名正确解释
  • 网站权重接口阿里巴巴国际站
  • 《网站建设与管理》方案设计培训班学费一般多少
  • 网站建设合同的性质不受限制的搜索引擎
  • 柯林建站程序站长统计是什么意思