如果我评论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();
}
我想编写一个不等待线程完成的函数。
我该如何解决这个工作示例?
#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();
}