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;
    }
}
Zingam
  • 4,498
  • 6
  • 28
  • 48

1 Answers1

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.

David Haim
  • 25,446
  • 3
  • 44
  • 78
  • 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 Sep 05 '16 at 12:50
  • @Zingam bad idea. chain these tasks rather than ask them if they are done. – David Haim Sep 05 '16 at 14:14
  • 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. – Zingam Sep 05 '16 at 19:29