저는 많은 사람들이이 질문을했지만, "많은 사람들"이 아니라는 것을 알고 있습니다. 이해하기 위해서는 좀 더 다른 설명이 필요합니다.스레드에서 "detach()"는 무엇을합니까? CPP
멤버 함수 "detach()"는 정확히 무엇을합니까? 내가 다음 코드 샘플을 실행하려고 :
#include <iostream>
#include <chrono>
#include <thread>
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(200));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
그리고 5 초 후에 모든 프로그램을 종료합니다. "분리"에 대한 모든 아이디어가 메인과 독립되어 있기 때문에 프로그램이 195 초 후에 닫히게 될 것이라고 생각했기 때문에 독립적 인 방법으로 분리 된 모든 therades가 종결 될 때까지 계속 실행해야합니다 ... 설명서를 읽고 여기로 왔습니다. 더 나은 설명 - 제발! :)
['std :: async'] (http://en.cppreference.com/w/cpp/thread/async)를 사용하면 편리합니다. –