Destructor of the connected std :: thread

The specification (? - derived from cppreference) indicates:

~ thread (); (since C ++ 11)

Destroys the stream object. If * it still has an associated thread (i.e. joinable () == true), std :: terminate () is called.

I checked that a call std::terminate()inside the thread interrupts the entire program.

So, to test the behavior of the destructor, 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; 
}

Waiting for the interruption of the entire process.

Nothing of the kind happened; the whole weekend was a message rearrangement, for example:

1 Directional flow:

Home is still alive!

I am using g ++: Theme model: posix, gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1 ~ 12.04)

Do I have a misunderstanding of the specification? Incorrect code? Or is g ++ not just meeting this specification?

+4
1

, thread_ptr.reset() thread_ptr.release(). release() , .. std::thread, , , .

+2

All Articles