#include <thread>
#include <string>
#include <vector>
#include <chrono>
using namespace std;
void f(const vector<string>& coll)
{
this_thread::sleep_for(1h);
//
// Is coll guaranteed to be valid before exiting this function?
//
}
int main()
{
{
vector<string> coll(1024 * 1024 * 100);
thread(f, coll).detach();
}
//
// I know std::thread will copy arguments into itself by default,
// but I don't know whether these copied objects are still valid
// after the std::thread object has been destroyed.
//
while (true);
}
참조로 인수를 std :: thread 함수로 전달하는 것이 안전합니까?참조로 인수를 std :: thread 함수로 전달하는 것이 안전합니까?
짧은 대답 : 예, 완전히 안전하고 괜찮습니다. – DeiDei
스레드에 대한 참조로 아무 것도 전달하지 않습니다. –
죄송합니다. 당신 말이 맞습니다. 나는 코드를 잘못 읽었다. – Rafael