如何检查是否一个std ::异步任务完成? [英] How to check if a std::async task is finished?
问题描述
在我的图形应用程序我要生成在另一个线程批的网格。因此,我使用异步调用的成员函数的std ::异步
。
In my graphics application I want to generate a batch meshes in another thread. Therefore I asynchrony call the member function using std::async
.
task = async(launch::async, &Class::Meshing, this, Data(...));
在我的更新循环我尝试检查,如果线程准备。如果是的话,我将网格发送到显卡,并开始下一个线程。如果没有,我会跳过这些操作。
In my update loop I try to check if the thread is ready. If yes, I will send the mesh to the video card and start the next thread. If not, I will skip these operations.
#include <future>
using namespace std;
class Class
{
public:
void Update()
{
if(task.finished()) // this method does not exist
{
Data data = task.get();
// ...
task = async(launch::async, &Class::Meshing, this, Data(/* ... */));
}
}
private:
struct Data
{
// ...
};
future<Data> task;
Data Meshing(Data data)
{
// ...
}
};
我如何检查异步线程没有完成在更新功能stucking?
How can I check if the asynchrony thread finished without stucking in the update function?
推荐答案
使用 未来:: wait_for()
。您可以指定超时,之后,得到了状态code。
Use future::wait_for()
. You can specify a timeout, and after that, get a status code.
task.wait_for(std::chrono::seconds(1));
这将返回 future_status ::准备
, future_status ::推迟
或 future_status: :超时
,所以你知道操作的状态。你还可以指定为0,超时有检查返回<击>立即行使>尽快
This will return future_status::ready
, future_status::deferred
or future_status::timeout
, so you know the operation's status. You can also specify a timeout of 0 to have the check return immediately as soon as possible.
这篇关于如何检查是否一个std ::异步任务完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!