<bdo id='7DDPd'></bdo><ul id='7DDPd'></ul>
    <i id='7DDPd'><tr id='7DDPd'><dt id='7DDPd'><q id='7DDPd'><span id='7DDPd'><b id='7DDPd'><form id='7DDPd'><ins id='7DDPd'></ins><ul id='7DDPd'></ul><sub id='7DDPd'></sub></form><legend id='7DDPd'></legend><bdo id='7DDPd'><pre id='7DDPd'><center id='7DDPd'></center></pre></bdo></b><th id='7DDPd'></th></span></q></dt></tr></i><div id='7DDPd'><tfoot id='7DDPd'></tfoot><dl id='7DDPd'><fieldset id='7DDPd'></fieldset></dl></div>
  1. <legend id='7DDPd'><style id='7DDPd'><dir id='7DDPd'><q id='7DDPd'></q></dir></style></legend>
  2. <tfoot id='7DDPd'></tfoot>

  3. <small id='7DDPd'></small><noframes id='7DDPd'>

      如何在C++中实现函数的超时

      How to implement timeout for function in c++(如何在C++中实现函数的超时)
      <tfoot id='HlWp2'></tfoot>
      <legend id='HlWp2'><style id='HlWp2'><dir id='HlWp2'><q id='HlWp2'></q></dir></style></legend>

        <bdo id='HlWp2'></bdo><ul id='HlWp2'></ul>

            <i id='HlWp2'><tr id='HlWp2'><dt id='HlWp2'><q id='HlWp2'><span id='HlWp2'><b id='HlWp2'><form id='HlWp2'><ins id='HlWp2'></ins><ul id='HlWp2'></ul><sub id='HlWp2'></sub></form><legend id='HlWp2'></legend><bdo id='HlWp2'><pre id='HlWp2'><center id='HlWp2'></center></pre></bdo></b><th id='HlWp2'></th></span></q></dt></tr></i><div id='HlWp2'><tfoot id='HlWp2'></tfoot><dl id='HlWp2'><fieldset id='HlWp2'></fieldset></dl></div>
              <tbody id='HlWp2'></tbody>

            <small id='HlWp2'></small><noframes id='HlWp2'>

                本文介绍了如何在C++中实现函数的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有函数 f;我想在启动 f 后抛出异常 1s.我无法修改 f().可以用c++实现吗?

                I have got function f; I want to throw exception 1s after start f. I can't modify f(). It it possible to do it in c++?

                try {
                   f();
                }
                catch (TimeoutException& e) {
                //timeout
                }
                

                推荐答案

                您可以创建一个单独的线程来运行调用本身,并在主线程中等待一个条件变量,该条件变量将由执行调用的线程发出信号f 一旦它返回.诀窍是在 1s 超时的情况下等待条件变量,这样如果调用时间超过超时时间,您仍然会醒来,知道它,并能够抛出异常 - 全部在主线程中.这是代码(现场演示 此处):

                You can create a separate thread to run the call itself, and wait on a condition variable back in your main thread which will be signalled by the thread doing the call to f once it returns. The trick is to wait on the condition variable with your 1s timeout, so that if the call takes longer than the timeout you will still wake up, know about it, and be able to throw the exception - all in the main thread. Here is the code (live demo here):

                #include <iostream>
                #include <chrono>
                #include <thread>
                #include <mutex>
                #include <condition_variable>
                
                using namespace std::chrono_literals;
                
                int f()
                {
                    std::this_thread::sleep_for(10s); //change value here to less than 1 second to see Success
                    return 1;
                }
                
                int f_wrapper()
                {
                    std::mutex m;
                    std::condition_variable cv;
                    int retValue;
                
                    std::thread t([&cv, &retValue]() 
                    {
                        retValue = f();
                        cv.notify_one();
                    });
                
                    t.detach();
                
                    {
                        std::unique_lock<std::mutex> l(m);
                        if(cv.wait_for(l, 1s) == std::cv_status::timeout) 
                            throw std::runtime_error("Timeout");
                    }
                
                    return retValue;    
                }
                
                int main()
                {
                    bool timedout = false;
                    try {
                        f_wrapper();
                    }
                    catch(std::runtime_error& e) {
                        std::cout << e.what() << std::endl;
                        timedout = true;
                    }
                
                    if(!timedout)
                        std::cout << "Success" << std::endl;
                
                    return 0;
                }
                

                这篇关于如何在C++中实现函数的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                  • <small id='XHUXR'></small><noframes id='XHUXR'>

                  • <i id='XHUXR'><tr id='XHUXR'><dt id='XHUXR'><q id='XHUXR'><span id='XHUXR'><b id='XHUXR'><form id='XHUXR'><ins id='XHUXR'></ins><ul id='XHUXR'></ul><sub id='XHUXR'></sub></form><legend id='XHUXR'></legend><bdo id='XHUXR'><pre id='XHUXR'><center id='XHUXR'></center></pre></bdo></b><th id='XHUXR'></th></span></q></dt></tr></i><div id='XHUXR'><tfoot id='XHUXR'></tfoot><dl id='XHUXR'><fieldset id='XHUXR'></fieldset></dl></div>

                    <tfoot id='XHUXR'></tfoot>
                    <legend id='XHUXR'><style id='XHUXR'><dir id='XHUXR'><q id='XHUXR'></q></dir></style></legend>
                      <bdo id='XHUXR'></bdo><ul id='XHUXR'></ul>
                            <tbody id='XHUXR'></tbody>