Skip to content

Instantly share code, notes, and snippets.

@vittorioromeo
Created August 27, 2013 23:16
Show Gist options
  • Save vittorioromeo/6360272 to your computer and use it in GitHub Desktop.
Save vittorioromeo/6360272 to your computer and use it in GitHub Desktop.
Temporary lifetime fuckfuck
// Consider these code snippets:
struct Test { Test() { thread([this]{ /* do something */; }).detach(); }
int main()
{
Test{}; // <-- variable dies here! The "do something" inside the detached
// thread doesn't work as expected (undefined behavior?)
while(true) { this_thread::sleep(milliseconds(1)); }
}
// ---
struct Test { Test() { thread([this]{ /* do something */; }).detach(); }
int main()
{
Test t{};
while(true) { this_thread::sleep(milliseconds(1)); }
} // <-- variable dies here! The "do something" works as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment