Following program runs differently depending on the sleep mechanism used.
#include <ppltasks.h>
#include <chrono>
#include <thread>
#include <iostream>
#include <atomic>
#include <windows.h>
using namespace std;
#define MODERN_MAN
int main()
{
atomic<int> cnt;
concurrency::task_group tg;
for (int i =0; i<1000; ++i )
{
tg.run([&cnt](){cout << "."; cnt++;
#ifdef MODERN_MAN
this_thread::sleep_for(chrono::seconds(5));
#else
Sleep(5000);
#endif
});
}
tg.wait();
cout << cnt;
return 0;
}
Difference is that with sleep_for
from what I see tasks get scheduled in a way that 1 sleeping doesnt prevent others from running.
With Sleep
from what I see they block further tasks from running.
My questions are:
a) how can PPL thread pool do the clever trick that enables it to workaround sleep_for
( I thought it is a system call that tells OS (put this thread in the list of inactive threads for x seconds)
b) Is the behavior Im seeing here with sleep_for
guaranteed(aka is it defined that I wont get same behavior as with Sleep
)