【问题标题】:POSIX issue with mingw (sem_timedwait)mingw (sem_timedwait) 的 POSIX 问题
【发布时间】:2015-05-21 09:21:38
【问题描述】:

我正在使用 mingw:

GNU Make 4.1

为 x86_64-w64-mingw32 构建

我注意到 sem_timedwait 不起作用(它返回 -1)。 我对 sem_wait 或 sem_signal 没有意见。

代码示例:

struct timespec timeSpec;
int   rc = 0;
timeSpec.tv_sec = 1000;
timeSpec.tv_nsec = 0;
rc = sem_timedwait(pSemId,&timeSpec);

CC= x86_64-w64-mingw32-gcc

【问题讨论】:

  • errno 设置为什么?
  • printf("%s", strerror(errno)) --> "Unknown error" "Unknown error" 是errno的结果
  • errno 的实际数值会更有帮助,因为不能保证strerror() 对每个可能的值都有描述。有些描述完全令人困惑。有了errno,您可以检查 Mingw 标头以查看代码对应的错误。这应该有助于缩小您对问题的搜索范围。
  • printf("%d", errno) --> 138 error.h: #define ETIMEDOUT 138 但绝对没有发生超时!
  • "mingw" "sem_timedwait" "errno" "138" 的简单谷歌显示:#define ETIMEDOUT 138。我希望你能猜到其余的。 ;)

标签: time operating-system posix semaphore


【解决方案1】:

好的, 时间应该是相对的而不是绝对的。 tm = 时间(NULL); timeSpec.tv_sec = tm+10; timeSpec.tv_nsec = 0; rc = sem_timedwait(pSemId,&timeSpec);

它解决了问题。

【讨论】:

  • 很高兴您解决了这个问题!使用绝对时间非常迟钝,而其他所有类似函数都使用相对时间。
猜你喜欢
  • 1970-01-01
  • 2013-06-19
  • 2011-02-27
  • 2010-12-13
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 2011-07-10
  • 2011-05-02
相关资源
最近更新 更多