pthread_cond_timedwait() in Windows

pthread_cond_timedwait() in Windows

本文关键字:in Windows timedwait cond pthread      更新时间:2023-10-16

我尝试在我的代码中实现pthread功能。不幸的是,我无法正确实现功能pthread_cond_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 = %dn", rcTimedwait);
printf("errno = %d   GetLastError = %dn", errno, GetLastError());
printf("tv_sec = %d   tv_nsec = %dn", timeout.tv_sec, timeout.tv_nsec);
pthread_mutex_unlock(&mutex);
pthread_cond_destroy(&condVar);
pthread_mutex_destroy(&mutex);
return 0;
}

和输出:

rcTimedwait = 10060
errno = 0   GetLastError = 0
tv_sec = 1   tv_nsec = 0

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

pthread_cond_timedwait()返回了 10060,这看起来像 WSAETIMEDOUT 的值。我很惊讶该函数没有按预期返回 ETIMEDOUT。

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