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 6631775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:35:38+00:00 2026-05-25T22:35:38+00:00

With threads I know that terminate() is called when the thread-variable leaves scope: size_t

  • 0

With threads I know that terminate() is called when the thread-variable leaves scope:

size_t fibrec(size_t n) {
  return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}

int main() {
    std::thread th{ fibrec, 35 };
    // no join here
} // ~th will call terminate().

ths destructor will call terminate() when it leaves scope.

But what about futures? Where is the thread they run going? Is it detached? How is it ended?

#include <iostream>
#include <future> // async
using namespace std;

size_t fibrec(size_t n) {
    return n<2 ? 1 : fibrec(n-2)+fibrec(n-1);
}

struct Fibrec {
    size_t operator()(size_t n) { return fibrec(n); }
    const size_t name_;
    Fibrec(size_t name) : name_(name) {}
    ~Fibrec() { cerr << "~"<<name_<< endl; }
};

void execit() {
    auto f1 = async( Fibrec{33}, 33 );
    auto f2 = async( Fibrec{34}, 34 );
    // no fx.get() here !!!
}; // ~f1, ~f2, but no terminate()! Where do the threads go?

int main() {
    auto f0 = async( Fibrec{35}, 35 );
    execit();
    cerr << "fib(35)= " << f0.get() << endl;
}

When execit() is left the futures f1 and f2 are destroyed. But their threads should still be running? The destructor of Fibrec is called, of course. But where are the threads going? The program does not crash, so I guess, the become joined? Or maybe detached? Or are they stopped or canceled? I believe that is not trivially done in C++11?

  • 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-05-25T22:35:38+00:00Added an answer on May 25, 2026 at 10:35 pm

    The future is the result of the async operation, it’s is not a thread by itself. The async function spawns a new thread to do the computation, and when that is finished, the result is written to a future object.

    Depending on the implemented policy, you will either have to call .get() or .wait() on the future to get the result. Invoking this will do the work on the thread on which it is called

    If policy is std::launch::async then runs INVOKE(fff,xyz…) on its
    own thread. The returned std::future will become ready when this
    thread is complete
    , and will hold either the return value or exception
    thrown by the function invocation. The destructor of the last future
    object associated with the asynchronous state of the returned
    std::future shall block until the future is ready.

    If policy is std::launch::deferred then fff and xyz… are stored
    in the returned std::future as a deferred function call. The first
    call to the wait() or get() member functions on a future that shares
    the same associated state will execute INVOKE(fff,xyz…)
    synchronously on the thread that called wait() or get().

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

Sidebar

Related Questions

I wish to know is there any way that I can create the threads
Firstly I know that there are many question and solutions to correct thread marshalling
I know the thread which says that rebase is for small changes of teamMates,
My program has a main thread that takes command input from a user. Separately,
I would like to know how threads are handled on the server side using
I don't know about threads in Java. I like to know what is happening
I know there have been a few threads on this before, but I have
I know how to use SwingWorker threads, but I still don't have precise criteria
I know there are several threads and posts regarding this issue in the internet
I wanted to know how to use threads in Cocoa. I'm new to this

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.