I am confuse to see this small program aborts if I comment t.join()
void my_thread()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "thread completed\n";
}
void main()
{
std::cout << "starting thread...\n";
std::thread t(my_thread);
t.join(); //<=== my program aborts when I comment this line
std::cout << "press a key to quit..." << std::endl;
std::getchar();
}
I want to write a function that does not wait the thread to complete.
How should I fix this working example?
#include <iostream>
#include <thread>
#include <chrono>
void my_thread()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "thread completed\n";
}
void send_message()
{
std::cout << "starting thread...\n";
std::thread t(my_thread);
t.join(); //<=== the function aborts when I comment this line
}
void main()
{
send_message();
std::cout << "press a key to quit..." << std::endl;
std::getchar();
}
Aucun commentaire:
Enregistrer un commentaire