C++ Windows中的pthread_cond_timedwait()

C++ Windows中的pthread_cond_timedwait(),c++,windows,pthreads,C++,Windows,Pthreads,我尝试在代码中实现pthread功能。不幸的是,我无法正确实现函数pthread\u cond\u timedwait()。 在Linux中,一切正常。但在Windows中,此函数始终返回错误代码10060。以下是我的简单代码: #include <fstream> #include <Windows.h> #define HAVE_STRUCT_TIMESPEC #include <pthread.h> int main() { int rcTimed

我尝试在代码中实现
pthread
功能。不幸的是,我无法正确实现函数
pthread\u cond\u timedwait()
。 在Linux中,一切正常。但在Windows中,此函数始终返回错误代码10060。以下是我的简单代码:

#include <fstream>
#include <Windows.h>
#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>

int main()
{
  int rcTimedwait = 0;
  struct timespec timeout;
  pthread_mutex_t mutex;
  pthread_cond_t condVar;
  pthread_mutex_init(&mutex, NULL);
  pthread_cond_init(&condVar, NULL);
  timeout.tv_sec = 1;
  timeout.tv_nsec = 0;
  SetLastError(0);
  errno = 0;
  pthread_mutex_lock(&mutex);
  rcTimedwait = pthread_cond_timedwait(&condVar, &mutex, &timeout);
  printf("rcTimedwait = %d\n", rcTimedwait);
  printf("errno = %d   GetLastError = %d\n", errno, GetLastError());
  printf("tv_sec = %d   tv_nsec = %d\n", timeout.tv_sec, timeout.tv_nsec);
  pthread_mutex_unlock(&mutex);
  pthread_cond_destroy(&condVar);
  pthread_mutex_destroy(&mutex);

  return 0;
}

提前感谢您的帮助,并为我的英语感到抱歉。

pthread\u cond\u timedwait()
返回了10060,它看起来像的值。我很惊讶函数没有按预期返回ETIMEDOUT


无论如何,这是一个超时,这并不奇怪,因为在您的示例中没有其他线程向条件变量发送信号。

使用C++11线程。旁白:当您打印
errno
GetLastError()
值时,可以通过调用
printf()修改它们
。不清楚它在windows上返回超时的原因,您的代码是错误的
pthread_cond_timedwait
需要绝对时间,而不是延迟。修复后,两种平台上都会出现超时错误。请举例说明如何在Windows中填充struct timespec timeout。函数不会返回ETIMEDOUT,但会返回。在Windows E。。。错误代码是根据相应的WSAE。。。代码。
rcTimedwait = 10060
errno = 0   GetLastError = 0
tv_sec = 1   tv_nsec = 0