Reputation: 1723
The specification (? - got it from cppreference) states:
~thread(); (since C++11)
Destroys the thread object. If *this still has an associated running thread (i.e. joinable() == true), std::terminate() is called.
I've checked that calling std::terminate()
inside thread aborts the whole program.
So to check destructor behaviour I wrote this code:
#include <iostream>
#include <thread>
#include <memory>
int main() {
std::unique_ptr<std::thread> thread_ptr(new std::thread([](){
std::cout << "Starting thread: " << std::endl;
while(1) {}
}));
while(!thread_ptr->joinable()){}
std::cout << thread_ptr->joinable() << std::endl;
thread_ptr.release();
std::cout << "Main is still alive!" << std::endl;
return 0;
}
Expecting abort of whole process.
No such thing happened, all output was a permutation of messages, like:
1Starting thread:
Main is still alive!
I'm using g++ : Thread model: posix, gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~12.04)
Do I have wrong understanding of specification? Wrong code? Or g++ isn't just compliant with this specification?
Upvotes: 4
Views: 412
Reputation: 171097
You probably meant thread_ptr.reset()
instead of thread_ptr.release()
. release()
relinquishes ownership of the pointer, i.e. you leak the std::thread
instance and its destructor is thus never called.
Upvotes: 2