我有一种情况,如果要花费太多时间来完成,我想取消一个线程。为此,我正在使用第二个线程,它等待第一个线程完成,但不超过几秒钟。 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/Linux
g++(GCC)4.4.7 20120313(红帽4.4.7-4)
编译命令:g++ -o execute_binary -pthread -lrt source_code.cpp
最佳答案
编辑:我首先建议不要使用pthread_cond_timedwait,但是我认为在这种情况下可以,因此第一个线程的等待时间不会超过所需的时间,尽管我会检查设置为“finished”的标志,而不是检查返回值由第二个线程保护,并由互斥体保护。
您的示例中的问题在于,互斥锁是由第一个线程获取的,而互斥锁是由pthread_cond_timedwait()
调用释放的。然后它被第二个线程占用,从而阻塞了第一个线程,直到第二个线程最后释放互斥锁为止。