【发布时间】:2014-03-26 16:10:32
【问题描述】:
我有一种情况,我想取消一个线程,如果它需要太多才能完成。为此,我使用第二个线程等待第一个线程完成,但不超过几秒钟。 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 executable_binary -pthread -lrt source_code.cpp
【问题讨论】:
-
尝试在发出条件信号之前将互斥锁锁定在第二个线程中。互斥锁被锁定时,第一个线程无法唤醒。
标签: c++ linux multithreading