I have this code:
#include "stdafx.h"
#include <iostream>
#include <future>
#include <chrono>
int main()
{
bool bExit = false;
int iSeconds = 0;
std::thread t([&]()
{
std::this_thread::sleep_for(std::chrono::seconds{ 10 });
bExit = true;
});
while (!bExit)
{
std::this_thread::sleep_for(std::chrono::seconds{ 1 });
std::cout << "SECONDS PASSED: " << ++iSeconds << std::endl;
};
//t.join();
std::cout << "PRESS ENTER TO EXIT";
getchar();
return 0;
}
I don't understand why if I don't call t.join() I get an "abort() has been called" error. I've checked with the debugger and the new thread ceases to exist as soon the lambda exits. Apart from the specific error, is it safe using lambdas in this manner and never call joins?
My ultimate goal is to create some one time execution launch and forget lambdas for delayed execution, and maybe std::thread isn't the best way, so if someone has some advise on a better way to implement it...
Many thanks in advance