Spawning futures as an alternative to multithreading I was trying to do a sample threading program, where the spawning of thread is in a while loop. I DO NOT want to generate multiple number of threads. The while loop should keep on running, irrespective of whether the thread completed execution or not. There is a call to the .get() function of the above thread within the while loop. Again, this has to be called only when the thread executed correctly.

My code is given below:

#include &lt;iostream&gt;
#include &lt;future&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;

int thread(int varJ)
{
    std::cout&lt;&lt;"\t\t"&lt;&lt;varJ&lt;&lt;std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    return varJ;  
}

int main()
{
    int varI = 0;
    std::future&lt;int&gt; future = std::async(std::launch::async, [](){return 0;});
    std::future_status status;
    bool flag = false;

    while(1)
    {
        status = future.wait_for(std::chrono::milliseconds(0));
        if(status == std::future_status::ready &amp;&amp; !flag)
        {
            future = std::async(std::launch::async, thread, varI);
            flag = true;
        }
        status = future.wait_for(std::chrono::milliseconds(0));
        if(status == std::future_status::ready &amp;&amp; flag)
        {
            std::cout &lt;&lt; "\t result is " &lt;&lt; future.get() &lt;&lt; '\n';
            future = std::async(std::launch::async, [](){return 0;});
            flag = false;
        }
        varI++;
    }
}


The output obtained:

    0
 result is 0
    865
 result is 865
    1723
 result is 1723
    2632
 result is 2632
    3590
 result is 3590
    4551
 result is 4551
    5509
 result is 5509


The output is as I expected it to be. But is this the way to code or is there a better way?

I am not pleased to use a flag in between the code, calling future.wait_for a lot many times and especially using the dummy thread that returns 0.
I see some things that may help you improve your code.

Simplify your code using literals

There is nothing intrinsically wrong with std::chrono::milliseconds(0) but I would prefer to read (and to write) 0ms instead.  This can be done by adding this line to within main:

using namespace std::literals::chrono_literals;


Use more meaningful variable names

I understand that it's just a toy program, but I really think we can do better than varI and varJ!  Since it's just a freerunning counter, why not just call it count?

Understand the threading model

It's important to realize that std::future was designed to hold not only the future value (or exception) but also a flag for the future_status.  This renders the current code's flag unnecessary.  Here's a more concise rewrite of main:

int main()
{
    using namespace std::literals::chrono_literals;
    int count = 0;
    std::future&lt;int&gt; future = std::async(std::launch::async, thread, count);
    while(1) {
        if (future.wait_for(0ms) == std::future_status::ready) {
            std::cout &lt;&lt; "\t result is " &lt;&lt; future.get() &lt;&lt; '\n';
            future = std::async(std::launch::async, thread, count);
        }
        ++count;
    }
}


Beware of sharing unsynchronized variables

Right now, both the thread running main and the thread running the function write to std::cout.  The way the code is currently structured (both in the original and in the rewrite above), they do not contend for that same variable but they could because there currently is no synchronization.  The effect is that the output of the two threads could easily be interleaved.  The easiest way to prevent this is to only access cout from a single thread.