gpt4 book ai didi

c++ - Windows 中的 pthread_cond_timedwait()

转载 作者:可可西里 更新时间:2023-11-01 10:13:06 26 4
gpt4 key购买 nike

我尝试在我的代码中实现 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 = %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;
}

和输出:

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

在此先感谢您的帮助,对不起我的英语

最佳答案

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

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

关于c++ - Windows 中的 pthread_cond_timedwait(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com