Created
          August 27, 2013 23:16 
        
      - 
      
- 
        Save vittorioromeo/6360272 to your computer and use it in GitHub Desktop. 
    Temporary lifetime fuckfuck
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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