If to run this program using www.ideone.com
#include <iostream>
#include <thread>
#include <utility>
#include <stdexcept>
class scoped_thread
{
private:
std::thread t;
public:
explicit scoped_thread( std::thread t ) : t( std::move( t ) )
{
if ( not this->t.joinable() )
{
throw std::logic_error( "No thread" );
}
}
~scoped_thread()
{
t.join();
}
scoped_thread( const scoped_thread & ) = delete;
scoped_thread & operator =( const scoped_thread & ) = delete;
};
void h()
{
std::cout << "h() is running\n";
for ( size_t i = 0; i < 10000; i++ );
std::cout << "exiting h()\n";
}
void f()
{
scoped_thread t( std::thread( h ) );
}
int main()
{
f();
std::thread t( h );
t.join();
return 0;
}
then the output is
h() is running
exiting h()
that corresponds to the thread t
launced in main.
However a simialr output from the thread launched using the class scoped_thread
is absent. Why?