- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有这样一种情况,如果一个线程需要太多时间才能完成,我想取消它。为此,我使用第二个线程等待第一个线程完成,但不会超过几秒。 pthread_cond_timedwait() 函数似乎非常适合我的使用场景,但它的行为似乎并不像我预期的那样。更具体地说,即使 pthread_cond_timedwait() 函数返回 ETIMEDOUT,它也只是在它应该取消的线程完成后才这样做,这违背了整个目的。
这是我的测试代码:
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <cstring>
#define WAIT_INTERVAL 5
#define THREAD_SLEEP 10
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
pthread_t t1;
pthread_t t2;
void* f1(void*);
void* f2(void*);
int main()
{
pthread_create(&t1, NULL, &f1, NULL);
pthread_create(&t2, NULL, &f2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
std::cout << "Thread(s) successfully finished" << std::endl << std::flush;
exit(EXIT_SUCCESS);
}
void* f1(void*)
{
pthread_mutex_lock(&mutex);
timespec ts = {0};
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += WAIT_INTERVAL;
std::cout << __FUNCTION__ << ": Waiting for at most " << WAIT_INTERVAL << " seconds starting now" << std::endl << std::flush;
int waitResult = pthread_cond_timedwait(&condition, &mutex, &ts);
if (waitResult == ETIMEDOUT)
{
std::cout << __FUNCTION__ << ": Timed out" << std::endl << std::flush;
int cancelResult = pthread_cancel(t2);
if (cancelResult)
{
std::cout << __FUNCTION__ << ": Could not cancel T2 : " << strerror(cancelResult) << std::endl << std::flush;
}
else
{
std::cout << __FUNCTION__ << ": Cancelled T2" << std::endl << std::flush;
}
}
std::cout << __FUNCTION__ << ": Finished waiting with code " << waitResult << std::endl << std::flush;
pthread_mutex_unlock(&mutex);
}
void* f2(void*)
{
pthread_mutex_lock(&mutex);
std::cout << __FUNCTION__ << ": Started simulating lengthy operation for " << THREAD_SLEEP << " seconds" << std::endl << std::flush;
sleep(THREAD_SLEEP);
std::cout << __FUNCTION__ << ": Finished simulation, signaling the condition variable" << std::endl << std::flush;
pthread_cond_signal(&condition);
pthread_mutex_unlock(&mutex);
}
我从上面的代码得到的输出是:
f1: Waiting for at most 5 seconds starting now
f2: Started simulating lengthy operation for 10 seconds
f2: Finished simulation, signaling the condition variable
f1: Timed out
f1: Could not cancel T2 : No such process
f1: Finished waiting with code 110
Thread(s) successfully finished
鉴于这是我第一次使用 POSIX 线程,我想我遗漏了一些可能非常明显的东西。
我已经阅读了大量关于此的教程、文章和答案,但没有一个涉及我的用例,也没有一个提供任何提示。
请注意,为简洁起见,我删除了一些处理 pthread_cond_timedwait 手册中提到的谓词的代码,因为这不会改变任何行为。
我在 CentOS 6.5 机器上使用 POSIX 线程。我的开发测试环境:2.6.32-431.5.1.el6.centos.plus.x86_64 #1 SMP x86_64 x86_64 x86_64 GNU/Linuxg++ (GCC) 4.4.7 20120313 (红帽 4.4.7-4)
编译命令:g++ -o executable_binary -pthread -lrt source_code.cpp
最佳答案
编辑:我首先建议不要使用 pthread_cond_timedwait,但我认为在这种情况下没关系,因此第一个线程不会等待超过需要的时间,尽管我不会检查返回值,而是检查“完成”标志,它由第二个线程设置并受互斥锁保护。
您的示例中的问题是互斥体被第一个线程占用,互斥体由 pthread_cond_timedwait()
调用释放。然后它被第二个线程占用,从而阻塞第一个线程直到第二个线程最后释放互斥体。
关于c++ - pthread_cond_timedwait() 用于取消冗长任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22666883/
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
我是一名优秀的程序员,十分优秀!