#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