I have this code, which works as I wanted but I don\'t understand exactly why. Thinking about a stack in C, C++, I\'d guess that the p variable will be on the stack on each call
How does the closure of the thread captures it and more over, captures the correct value every time?
That is compiler magic. Simply because the p
parameter is being used by the lambda the compiler treats it differently. p
is not placed on the stack but on the heap. That is why it still exists after callme()
has terminated.