3

Does newly created task from C++ ppl library executes automatically or is there any mechanism needed in order to initiate the execution of aforementioned task?

Artur
  • 181
  • 1
  • 9

3 Answers3

1

The task is scheduled immediately.

concurrency::task constructor calls
_TaskInitMaybeFunctor which calls
_TaskInitWithFunctor<_ReturnType, _Function> which calls
_ScheduleTask which calls
_M_TaskCollection._ScheduleTask which calls (unless you've proveded your own schedueler)
_DefaultPPLTaskScheduler().schedule; which calls
(new _PPLTaskChore{ _Proc, _Param })->_Schedule; which calls
_Schedule_chore which calls
__crtCreateThreadpoolWork + _Reschedule_chore which calls
__crtSubmitThreadpoolWork which calls
SubmitThreadpoolWork

which submits the task into the win32 threadpool. So yes, the task IS scheduled immediately.

David Haim
  • 25,446
  • 3
  • 44
  • 78
0

You don't need to do anything to start the task.

Try the code like this:

#include "stdafx.h" // Windows.h for Sleep
#include <ppltasks.h>
#include <iostream>

using namespace concurrency;
using namespace std;

int main()
{
    // Create a task.
    task<int> t([]()
    {
        cout << "Task Running\n";
        return 42;
    });

    cout << "Task created\n";
    Sleep(5000L);
}

and you will see this in the console output, before program exits:

Task Created
Task Running

Without Sleep() program would exit right away and destroy the task. Instead of Sleep() you can put some CPU intensive operation, say a loop within a loop that would keep the program from exiting right away. Or just add:

std::cin.get();

The result would be the same. You don't need to call get() or wait() but, if you want to catch exceptions or handle cancellation, you will need to add continuation. While debugging you can launch Parallel Stacks window and you will see that Task Scheduler has created TPP worker thread, besides the main thread.

Tony
  • 1,566
  • 2
  • 15
  • 27
-1

No, it doesn't start automatically:

#include <ppltasks.h>
#include <iostream>

using namespace concurrency;
using namespace std;

int wmain()
{
    // Create a task.
    task<int> t([]()
    {
        cout << "Task Running" << endl;
        return 42;
    });

    cout << "Task created " << endl;

    wcout << "The result is " <<  t.get() << endl;    
}

The output of the above code will be

Task created
Task Running
The result is 42

If you comment out t.get(), the task will not run at all. You can also use t.wait() to force the task execution.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • #SingerOfTheFall so how one starts a task in a non-blocking fashion? As far as I know get and wait will block the thread that task is running in. – Artur Oct 22 '15 at 13:04
  • @DavidHaim yes, without t.get() the program exits before much can happen. BTW David, unrelated to this, I've seen your comments on how C++ REST is very slow. Is there any question where you give more details and, if not, can you suggest what question to ask so you can add some details? Thanks. – Tony Jan 08 '17 at 17:40