C++ 为c/c设置超时++;函数调用
假设我的主函数调用一个外部函数veryslow() 现在,我想在main中使用very_slow的调用部分,以便veryslow在超出时间限制时终止。像这样的C++ 为c/c设置超时++;函数调用,c++,timeout,C++,Timeout,假设我的主函数调用一个外部函数veryslow() 现在,我想在main中使用very_slow的调用部分,以便veryslow在超出时间限制时终止。像这样的 int main(){... call_with_timeout(veryslow, 0.1);...} 实现这一目标的简单方法是什么?我的操作系统是Ubuntu 16.04。您可以在新线程中调用此函数,并设置超时以终止线程,它将结束此函数调用 POSIX示例如下: #include <stdio.h> #include &
int main(){... call_with_timeout(veryslow, 0.1);...}
实现这一目标的简单方法是什么?我的操作系统是Ubuntu 16.04。您可以在新线程中调用此函数,并设置超时以终止线程,它将结束此函数调用 POSIX示例如下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
pthread_t tid;
// Your very slow function, it will finish running after 5 seconds, and print Exit message.
// But if we terminate the thread in 3 seconds, Exit message will not print.
void * veryslow(void *arg)
{
fprintf(stdout, "Enter veryslow...\n");
sleep(5);
fprintf(stdout, "Exit veryslow...\n");
return nullptr;
}
void alarm_handler(int a)
{
fprintf(stdout, "Enter alarm_handler...\n");
pthread_cancel(tid); // terminate thread
}
int main()
{
pthread_create(&tid, nullptr, veryslow, nullptr);
signal(SIGALRM, alarm_handler);
alarm(3); // Run alarm_handler after 3 seconds, and terminate thread in it
pthread_join(tid, nullptr); // Wait for thread finish
return 0;
}
#包括
#包括
#包括
#包括
pthread_t tid;
//您的功能非常慢,它将在5秒后完成运行,并打印退出消息。
//但如果我们在3秒钟内终止线程,退出消息将不会打印。
void*veryslow(void*arg)
{
fprintf(stdout,“输入veryslow…\n”);
睡眠(5);
fprintf(标准输出,“退出veryslow…\n”);
返回空ptr;
}
无效报警处理程序(int a)
{
fprintf(标准输出,“输入报警处理程序…\n”);
pthread_cancel(tid);//终止线程
}
int main()
{
pthread_create(&tid,nullptr,veryslow,nullptr);
信号(信号机、报警处理器);
alarm(3);//在3秒后运行alarm_处理程序,并终止其中的线程
pthread_join(tid,nullptr);//等待线程完成
返回0;
}
您可以使用带超时的future
std::future<int> future = std::async(std::launch::async, [](){
veryslow();
});
std::future_status status;
status = future.wait_for(std::chrono::milliseconds(100));
if (status == std::future_status::timeout) {
// verySlow() is not complete.
} else if (status == std::future_status::ready) {
// verySlow() is complete.
// Get result from future (if there's a need)
auto ret = future.get();
}
std::future-future=std::async(std::launch::async,[](){
veryslow();
});
标准:未来状态;
状态=未来。等待(std::chrono::毫秒(100));
如果(状态==标准::未来状态::超时){
//verySlow()未完成。
}else if(状态==std::future\u状态::ready){
//verySlow()已完成。
//从未来获得结果(如果需要)
auto-ret=future.get();
}
请注意,没有内置的方法来取消异步任务。您必须在verySlow
本身内部实现该功能
请参见此处了解更多信息:
我会将指向接口的指针传递到函数中,并要求返回一个指针。有了它,我将启用双向通信来执行所有必要的任务——包括超时和超时通知。修改
veryslow()
以持续时间作为参数,然后在必要时退出它。“没有内置的方法来取消异步任务”,那么在这种情况下使用未来有什么好处呢?这只会让它变得更复杂。@SidS:好吧,您可以继续并忽略调用。并且调用线程不会阻塞。基本上,您可以获得异步编程的所有好处,但它仍然在运行。问题是如何让它在一段时间后停止运行。@SidS:最好的办法是修改verySlow
本身。终止线程从来不是一个好主意,但如果您坚持,请在线程中运行verySlow
,等待调用线程超时,然后在线程仍在运行时终止线程。我同意,最好的办法是修改verySlow()
。在这种情况下不需要多线程。这是一个好主意,但需要更多细节才能成为有用的答案。是的,我没有给出实际的代码示例,因为OP没有指定他正在使用的操作系统。线程相关的API在不同的操作系统中是不同的。
std::future<int> future = std::async(std::launch::async, [](){
veryslow();
});
std::future_status status;
status = future.wait_for(std::chrono::milliseconds(100));
if (status == std::future_status::timeout) {
// verySlow() is not complete.
} else if (status == std::future_status::ready) {
// verySlow() is complete.
// Get result from future (if there's a need)
auto ret = future.get();
}