检查线程是否在c ++ 11中完成? [英] Check if thread is finished in c++11?

查看:77
本文介绍了检查线程是否在c ++ 11中完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来知道我正在运行的线程是否完成。如果尚未完成,请等待,如果完成,请打印一条成功消息。

I need a way to know if the thread I am running is finished or not. If it is not finished it wait and if it is finished print a successful message.

我在库中的c ++ 11中找不到方法或类似的方法线。
我无法设置全局变量,因为在线程内部我正在使用 execvp ,如果成功,它不会返回。

I don't find a method or something like that in c++11 from library thread. I can't set a global variable because inside thread I am using execvp and it does not return if successful.

有什么办法吗?方法或标志或其他任何东西。

Is there any way to do that? A method or flag or anything else.

编辑:为了清楚起见,我想编写一个函数来检查如果线程完成。

To make it clear I want to write a function that checks if thread is finished.

推荐答案

您可以使用 C ++ 11期货和承诺


期货是在线程之间传递值的高级机制,它允许线程等待结果可用而不必直接管理锁。

Futures are a high level mechanism for passing a value between threads, and allow a thread to wait for a result to be available without having to manage the locks directly.

期货相对于使用普通线程的好处是:

The benefits of futures over using plain threads are:


  • 未来会返回价值。

  • 您可以等待超时。

当然,这可以通过普通线程来完成,但是为什么要用C ++ 11重新实现。

Surely, this can be done with plain threads, but why reimplement a wheel with C++11.

这篇关于检查线程是否在c ++ 11中完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆