Consider the case below
The name string is moved as an argument to the thread.
void start(std::string&& name) {
t = std::thread{&ThreadRunner::run, this, std::forward<std::string>(name)};
}
The thread's run function also takes a rvalue reference.
void run(std::string&& name) {
const auto errnum = pthread_setname_np(t.native_handle(), name.c_str());
if (errnum != 0) {
std::cout << "ERROR " << std::endl;
}
}
The thread is created via the start function as below:
ThreadRunner r;
r.start(std::string("somename"));
The question is. Could it be possible that the std::string accessed in the functionrun via pthread_setname_np can be junk because the temporary went out of scope when it's scope ended?
Demo In the above demo, after call ended, is it guranteed that the somename string is valid in the function run?
Aucun commentaire:
Enregistrer un commentaire