0

Is this the correct way to test if a task is done?

const concurrency::task<void> voidTask;
if (voidTask != m_getInfoAsync)
{
    if (!m_getInfoAsync.is_done())
    {
        return 0;
    }
}
if (voidTask != m_getRangeAsync)
{
    if (!m_getRangeAsync.is_done())
    {
        return 0;
    }
}

1 Answer 1

0

although task::is_done is the correct way to test if the task is done, I suggest not using it. if is_done returns false, by the time you started acting on that fact, the task may be already done. this function is very racy, not to mention that this function probably requires some synchronization which may slow down the program.

instead, just chain a continuation or use co_await. handle finished task there.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using an asynchronous API (XboxLive) in a synchronous code (I can't do anything about that). This is a part of a function that tests if any previous operation was completed before starting a new one. As I have basically no knowledge about concurrecy RT (I am reading about it as I go) my first solution was with setting a variable before an async operation is started and setting the variable to false at the in the .then.
@Zingam bad idea. chain these tasks rather than ask them if they are done.
How could I do that? I am implementing an interface (virtual functions) getInfoAsync(), isAsyncComplete(), getRangeAsync(). Async in that case means that these tasks are running in a separate thread. They are invoked by a trigger system. These interfaces are already implemented for several different platforms and different APIs. Anyway I'll keep your advice in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.