Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:19:29+00:00 2026-06-02T04:19:29+00:00

#include <iostream> #include <thread> using namespace std; thread&& launch(){ thread launchee([](){ this_thread::sleep_for(chrono::milliseconds(280)); cout<<HA!<<endl; });

  • 0
#include <iostream>
#include <thread>
using namespace std;

thread&& launch(){
    thread launchee([](){
        this_thread::sleep_for(chrono::milliseconds(280));
        cout<<"HA!"<<endl;
    });
    return move(launchee);
}

int main(int argc, char **argv){
    thread mine(launch());
    mine.join();
}

compiled with g++-4.6 -std=c++0x 1.cpp -o e1 -pthread

Outputs “terminate called without an active exception”, then the program aborts.

This should work, shouldn’t it??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:19:31+00:00Added an answer on June 2, 2026 at 4:19 am

    You want to return a thread by value, not an rvalue-reference:

    std::thread launch() {
        std::thread launchee([](){
            std::this_thread::sleep_for(std::chrono::milliseconds(280));
            std::cout<<"HA!"<<std::endl;
        });
        return launchee;
    }
    

    The reason is that std::move (and std::forward) are just a cast operation from an lvalue to an rvalue-reference, but unless you use that to actually move into some other object, the original object remains unmodified. In the original code you are then taking a reference to a local object and exiting the function, the std::thread inside the function is then destroyed and it calls terminate (as per contract of std::thread, for any other object it would just return a dangling reference to a destroyed object).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <boost/thread.hpp> using std::endl; using std::cout; using namespace boost; mutex running_mutex; struct
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
Just read on an internal university thread: #include <iostream> using namespace std; union zt
When compiling the following code: #include <iostream> #include <thread> using namespace std; void hello()
#include <iostream> using namespace std; // This first class contains a vector and a
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.