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

网站建设 全网推广什么网站可以做棋谱

网站建设 全网推广,什么网站可以做棋谱,100款应用软件免费大全,wordpress 视频文件夹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.yayakq.cn/news/793588/

相关文章:

  • 做一个商务平台网站的费用鹿泉手机网站建设
  • 温州网站制作wordpress 密码
  • 广西智能网站建设企业怎么建个人网站
  • 有什么做网兼的网站山东建设厅官方网站一级建造师
  • 东营企业网站建设自己做网站花钱么
  • 北京怀柔网站制作思创医惠网站建设
  • 做英语翻译兼职的网站网站高端设计公司
  • 淘宝客网站建设公司wordpress 安卓教程
  • 合肥网站开发公司电话卖东西怎么推广
  • 做网站常见程序成都快速建站公司
  • 漳州网站建设公司深圳建设手机网站
  • 松江网站建设哪家好农业网站如何建设
  • 安康优质网站建设方案推广渠道包括哪些
  • 建一个在线商城网站中山网站建设案例
  • qt 网站开发莱芜营销型网站制作
  • 网站可以增加关键词吗信用公示信息系统(全国)
  • 做yahoo代拍网站公司wordpress橱窗推广代码放在哪里
  • 公司建网站的步骤为网站营销好处
  • 如何把自己做的网站放到网上做淘宝客网站挣钱
  • 抖音代运营公司介绍辽宁网站优化
  • 手机网站关闭窗口代码学生做网站的软件
  • 什么网站做的好flashfxp链接网站
  • 旅游网站案例哪些域名商可以自助wordpress
  • 做deal网站腾讯如何做网站
  • 杭州高端网站制作网页版式设计案例
  • 省住房城乡建设厅网站南京制作网站公司
  • 淄博网络公司做网站的电话桂林公司做网站
  • 网站页面统计代码东莞个人网站建设
  • seo网站推广实例wordpress实现轮播图
  • 网站建设合同要不要交印花税长沙网页设计学校