0

I'm learning to use tasks in C++. I don't understand why my task continuation never gets executed:

#include <iostream>
#include <ppltasks.h>
using namespace concurrency;

const concurrency::task<void> f1()
{
    std::cout << "Inside f1() \n";
    concurrency::task<void> rv;
    return rv;
}

const concurrency::task<void> f2()
{
    std::cout << "Inside f2() \n";
    concurrency::task<void> rv;
    return rv;
}

const concurrency::task<void> f3()
{
    std::cout << "Inside f3() \n";
    concurrency::task<void> rv;
    return rv;
}

int main()
{
    std::cout << "The BEGIN !\n"; 

    auto myTask = create_task(f1).then([&]() {  std::cout << "Affter         f1() \n"; });
    auto mytask2 = create_task(f2) && create_task(f3).then([=]() {      std::cout << "Affter f3() \n"; });

    myTask.then([&]() { std::cout << "Affter f1() \n"; });

    for (int i = 0; i < 10; i++)
    {
        std::cout << "The END !\n\n";
    }
}

Here is what the program produces:

The BEGIN !
Inside f1()
Inside f3()
Inside f2()
The END !

The END !

The END !

The END !

The END !

The END !

The END !

The END !

The END !

The END !

Edit:

After understanding better the tasks, I changed the program to this:

#include "pch.h"
#include <iostream>
#include <ppltasks.h>
using namespace concurrency;

task<void> f1()
{
    return create_task([] {std::cout << "Inside f1() \n"; }).then([] {std::cout << "Continuation of f1() \n"; });
}

task<void> f2()
{
    return create_task([] {std::cout << "Inside f2() \n"; }).then([] {std::cout << "Continuation of f2() \n"; });
}


task<void> f3()
{
    return create_task([] {std::cout << "Inside f3() \n"; }).then([] {std::cout << "Continuation of f3() \n"; });
}


int main()
{
    std::cout << "The BEGIN !\n"; 

    auto myTask1 = f1();
    auto myTask2 = f2();
    auto myTask3 = f3();

    //  use get() to wait for all 3 tasks and their continuations to be done
    (myTask1 && myTask2 && myTask3).get();

    std::cout << "The END !\n";
}

Which now seems to work, and gives this result:

The BEGIN !
Inside f1()
Inside f2()
Inside f3()
Continuation of f2()
Continuation of f3()
Continuation of f1()
The END !

So if I understand it well now, I cannot add a continuation separately to a task, like I tried to do in my first code version?

EDIT2:

Ok, I was wrong, we can very well add a continuation separately, like this:

int main()
{
    std::cout << "The BEGIN !\n"; 

    auto myTask1 = f1();
    auto myTask2 = f2();
    auto myTask3 = f3();

    auto completeTask = (myTask1 && myTask2 && myTask3).then([] {std::cout << "All tasks done !!! \n"; });
    
    //  use get() to wait for all 3 tasks and their continuations to be done
    completeTask.get();

    std::cout << "The END !\n";
}

Which will produce this result:

The BEGIN !
Inside f1()
Inside f2()
Inside f3()
Continuation of f1()
Continuation of f3()
Continuation of f2()
All tasks done !!!
The END !

Now I finally have exactly what I wanted, thank you all.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Lord Riton
  • 25
  • 5

0 Answers0