- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我尝试在我的代码中实现 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/
void wait(int timeInMs) { struct timespec timeToWait; timeToWait.tv_sec = 5; timeToWait.
void wait(int timeInMs) { struct timespec timeToWait; timeToWait.tv_sec = 5; timeToWait.
线程调用pthread_cond_timedwait 并返回ETIMEDOUT 后,线程是否拥有互斥体? 我最初认为不,但看起来我们必须调用pthread_mutex_unlock,即使在pthrea
所以我试图了解 pthread_cond_timedwait() 是如何工作的,因为我在我的项目上进行同步时遇到了一些问题。这是我想出的代码,但它并没有像我想象的那样工作。我的目标是打印时间,等待 2
我在 pthread_cond_timedwait() 上遇到了一个奇怪的问题:根据 POSIX 规范,它是一个取消点。但是,当我在线程上调用 pthread_cancel() 时,它永远不会被取消!
下面的程序产生这个输出: $ ./test_condvar 9000 1343868189.623067126 1343868198.623067126 FIRST 1343868197.623132
我正在尝试将 pthread_cond_timedwait 用于毫秒 sleep 间隔,但我没有获得 sleep 持续时间。我的线程比我提到的睡得更多。下面是我的实现。如果我哪里错了,请告诉我。
我遇到了一个奇怪的问题。我有以下代码: dbg("condwait: timeout = %d, %d\n", abs_timeout->tv_sec, abs_timeout
据我了解,pthread_cond_timedwait 的使用方法是获取当前时间,然后计算如果条件未发出信号 pthread_cond_timedwait 应退出的绝对时间。 有没有一种简单的方法可以
我有这样一种情况,如果一个线程需要太多时间才能完成,我想取消它。为此,我使用第二个线程等待第一个线程完成,但不会超过几秒。 pthread_cond_timedwait() 函数似乎非常适合我的使用场
考虑以下完全符合 POSIX 标准的源代码: #include #include #include #include #include #include int main (int arg
我在极少数情况下看到 pthread_cond_timedwait() 返回 EINVAL 并导致我们的系统发生致命崩溃。我知道这意味着传入的参数之一必须无效,但是 mutex 或 cond 变量如何
我正在尝试使用 pthread_cond_timedwait 来等待超时,类似于 Java 的 wait(long timeout, int nanos)。我知道 Java 的 wait 使用相对超时
我将 pthread_cond_timedwait 与单片定时器一起使用。我想问一下我的示例中是否存在问题或原因是什么,有时 pthread_cond_timedwait 等待的时间超过指定的超时时间
我用谷歌找不到任何关于这方面的信息,所以我在这里发帖希望有人能提供帮助... 我的问题是 Windows pthread 函数 pthread_cond_timedwait()。当指示的时间过去后,该
这应该在一个循环(服务器)中工作,并将工作/查询委托(delegate)给一个有故障的库,这里由 longrun() 函数调用表示,给一个超时时间为 tmax=3s 的线程。我放置了同步变量,我试图等
我需要让一个线程等待直到任一个 超时已过,或者 一个变量被另一个线程改变 经过一些研究,我发现 pthreads 有 pthread_cond_timedwait,如果我要使用 pthreads,这在
我尝试在我的代码中实现 pthread 功能。不幸的是,我无法正确实现函数 pthread_cond_timedwait()。在 Linux 中一切正常。但在 Windows 中,此函数始终返回错误代
所以我搜索了关于堆栈溢出和其他资源的高低,但我无法理解与上述功能有关的一些事情。具体来说, 1)当pthread_cond_timedwait()因为定时器值用完而返回时,它如何自动重新获取互斥量。互
pthread_cond_timedwait 函数需要时间 timespec 结构中的绝对时间。 我应该使用什么时间函数来获取绝对时间。我在网上看到了很多例子,我发现几乎所有时间功能都被使用了。 (f
我是一名优秀的程序员,十分优秀!