已解决
pthread_cond_timedwait 修改系统时间竟会导致其提前结束
来自网友在路上 164864提问 提问时间:2023-11-01 17:38:17阅读次数: 64
最佳答案 问答题库648位专家为你答疑解惑
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; }
查看全文
99%的人还看了
相似问题
- 时态图根据时间轴动态播放热力图
- 微信小程序发货信息录入接口 错误上传时间非法,请按照 RFC 3339 格式填写?
- 时间序列预测实战(十七)PyTorch实现LSTM-GRU模型长期预测并可视化结果(附代码+数据集+详细讲解)
- 设置指定时间之前的时间不可选
- C#关于TimeSpan结构的使用和获取两个时间差
- c++ 获取当前时间(精确至秒、毫秒和微妙)
- ChatGpt3.5已经应用了一段时间,分享一些自己的使用心得.
- java 统计代码运行时间
- 个人博客添加访问人数以及访问时间-githubpage
- 多维时序 | MATLAB实现PSO-BiGRU-Attention粒子群优化双向门控循环单元融合注意力机制的多变量时间序列预测
猜你感兴趣
版权申明
本文"pthread_cond_timedwait 修改系统时间竟会导致其提前结束":http://eshow365.cn/6-29494-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!