首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >thread.join()超时

问thread.join()超时
EN

Stack Overflow用户
提问于 2012-03-31 02:20:38
回答 6查看 32K关注 0票数 34

是否可以为调用std::thread::join()设置超时时间?我希望处理线程运行时间过长或终止线程的情况。我可能会为多个线程(比如最多30个线程)执行此操作。

最好不要boost,但如果这是最好的方式,我会对boost解决方案感兴趣。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-03-31 03:14:13

std::thread::join()没有超时。但是,您可以将std::thread::join()仅仅看作是一个方便的函数。使用condition_variable,您可以在线程之间创建非常丰富的通信和协作,包括计时等待。例如:

代码语言:javascript
复制
#include <chrono>
#include <thread>
#include <iostream>

int thread_count = 0;
bool time_to_quit = false;
std::mutex m;
std::condition_variable cv;

void f(int id)
{
    {
    std::lock_guard<std::mutex> _(m);
    ++thread_count;
    }
    while (true)
    {
        {
        std::lock_guard<std::mutex> _(m);
        std::cout << "thread " << id << " working\n";
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(250));
        std::lock_guard<std::mutex> _(m);
        if (time_to_quit)
            break;
    }
    std::lock_guard<std::mutex> _(m);
    std::cout << "thread ended\n";
    --thread_count;
    cv.notify_all();
}

int main()
{
    typedef std::chrono::steady_clock Clock;
    std::thread(f, 1).detach();
    std::thread(f, 2).detach();
    std::thread(f, 3).detach();
    std::thread(f, 4).detach();
    std::thread(f, 5).detach();
    auto t0 = Clock::now();
    auto t1 = t0 + std::chrono::seconds(5);
    std::unique_lock<std::mutex> lk(m);
    while (!time_to_quit && Clock::now() < t1)
        cv.wait_until(lk, t1);
    time_to_quit = true;
    std::cout << "main ending\n";
    while (thread_count > 0)
        cv.wait(lk);
    std::cout << "main ended\n";
}

在这个例子中,main启动了几个线程来做工作,所有这些线程偶尔都会检查是否到了在互斥锁(也可以是原子互斥锁)下退出的时候。主线程还监视是否到了退出的时间(如果线程完成了所有工作)。如果main耗尽了耐心,他只会声明是时候退出了,然后等待所有线程在退出之前执行任何必要的清理。

票数 24
EN

Stack Overflow用户

发布于 2015-10-08 21:52:45

是的,这是可能的。Galik提出的解决方案如下所示:

代码语言:javascript
复制
#include <thread>
#include <future>
...
// Launch the thread.
std::thread thread(ThreadFnc, ...);
...
// Terminate the thread.
auto future = std::async(std::launch::async, &std::thread::join, &thread);
if (future.wait_for(std::chrono::seconds(5)) 
    == std::future_status::timeout) {

  /* --- Do something, if thread has not terminated within 5 s. --- */

}

但是,这实际上会启动执行thread.join()的第三个线程。

(注意:在thread加入并终止辅助线程之前,future的析构函数将一直阻塞。)

也许仅仅为了关闭另一个线程而启动一个线程并不是你想要的。还有另一种不需要辅助线程的可移植解决方案:

代码语言:javascript
复制
#include <thread>
#include <future>
...
// Launch the thread.
std::future<T_return>*  hThread 
  = new std::future<T_return>(std::async(std::launch::async, ThreadFnc, ...));
...
// Terminate the thread.
if (hThread->wait_for(std::chrono::seconds(5)) 
    == std::future_status::timeout) {

  /* --- Do something, if thread has not terminated within 5 s. --- */

} else
  delete hThread;

其中T_return是线程过程的返回类型。此场景使用std::future / std::async组合,而不是std::thread。

注意,hThread是一个指针。当您对它调用delete操作符时,它将调用*hThread的析构函数并阻塞,直到线程终止。

我已经在Cygwin上用gcc 4.9.3测试了这两个版本。

票数 12
EN

Stack Overflow用户

发布于 2014-07-26 02:49:42

您可以使用std::async()为您提供std::future<>,并且可以在std::future上执行定时等待,而不是显式地使用线程

http://en.cppreference.com/w/cpp/thread/future/wait_for

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9948420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档