1. I try to get the thread ID of th by two methods as given in comments: (1) and (2), but the (2) always give the value 0. Why the results between (1) and (2) are different?
2. For comment (3), when I remove this line “th.join();” and compile using visual studio , there will be a debug error causing abort function. Why the debug error happens?
> Why the results between (1) and (2) are different?
After line 35 th.join(), there is no thread of execution associated with the object th.
On line 37, th.get_id() returns a default constructed thread id
(a value that indicates that there is no thread of execution associated with this thread object).
> when I remove this line “th.join();” ... there will be a debug error causing abort function.
> Why the debug error happens?
At the end of main(), when the destructor of the thread object th is invoked, it has an associated thread of execution - join() or detach() has not been called. In this situation, the destructor of the thread calls std::terminate(). The default handler for std::terminate() calls std::abort().