When reading a book about c++ multi-thread programming, I came across one example below.
void f(int i,std::string const& s);
void oops(int some_param)
{
char buffer[1024];
sprintf(buffer, "%i",some_param);
std::thread t(f,3,buffer);
t.detach();
}
In this case, it’s the pointer to the local variable buffer that’s passed through to the new thread, and there’s a significant chance that the function oops will exit before the buffer has been converted to a std::string on the new thread, thus leading to undefined behavior.
I know that it is the char* type argument buffer, not std::string is copied into the internal storage of thread t. What confuses me is that there's a chance the conversion from char* to std::string has not taken place even when thread t is already constructed. So when will the conversion take place? Is it just before the thread is scheduled to execute by the OS?