I'm trying to understand how to work with threads and this simple code crashes with this error:
the code:
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void thread1()
{
while (true)
{
cout << this_thread::get_id() << endl;
}
}
void main()
{
thread t1(thread1);
thread t2(thread1);
this_thread::sleep_for(chrono::seconds(1));
t1.detach();
t2.detach();
}
can someone please explain why it crashes after the detaches and how to fix this?