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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:09:00+00:00 2026-06-13T02:09:00+00:00

When testing threads in C++11 I have created the following example: #include <iostream> #include

  • 0

When testing threads in C++11 I have created the following example:

#include <iostream>
#include <thread>

class Foo {
public:
    Foo(void) {
        std::cout << "Constructor called: " << this << std::endl;
    }
    ~Foo(void) {
        std::cout << "Destructor called: " << this << std::endl;
    }
    void operator()() const {
        std::cout << "Operatior called: " << this << std::endl;
    }
};

void test_normal(void) {
    std::cout << "====> Standard example:" << std::endl;
    Foo f;
}

void test_thread(void) {
    std::cout << "====> Thread example:" << std::endl;
    Foo f;
    std::thread t(f);
    t.detach();
}


int main(int argc, char **argv) 
{
    test_normal();
    test_thread();

    for(;;);
}

Which prints the following:

enter image description here

Why is the destructor called 6 times for the thread? And why does the thread report different memory locations?

EDIT
When adding move and copy constructor output:

enter image description here

  • 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-13T02:09:01+00:00Added an answer on June 13, 2026 at 2:09 am

    Add a copy constructor and move constructor to your class.

    Foo(Foo const&) { std::cout << "Copy Constructor called: " << this << std::endl; }
    Foo(Foo&&) { std::cout << "Move Constructor called: " << this << std::endl; }
    

    Now if you run the code the output (on gcc 4.7.2) looks like this:

    ====> Standard example:
    Constructor called: 0xbff696ff
    Destructor called: 0xbff696ff
    ====> Thread example:
    Constructor called: 0xbff696ff
    Copy Constructor called: 0xbff696cf
    Move Constructor called: 0x93a8dfc
    Destructor called: 0xbff696cf
    Destructor called: 0xbff696ff
    Operator called: 0x93a8dfc
    Destructor called: 0x93a8dfc
    

    As you can see, the number of calls to the destructor matches the number of calls to the various constructors.

    I suspect gcc manages to elide a few of the copy / move construction calls that MSVC seems to be making, so there are fewer calls to destructor than your example.


    Furthermore, you can avoid the copy construction completely by std::moveing the Foo object to the thread constructor.

    In test_thread change the thread construction line to

    std::thread t(std::move(f));
    

    Now the output looks like this:

    ====> Standard example:
    Constructor called: 0xbfc23e2f
    Destructor called: 0xbfc23e2f
    ====> Thread example:
    Constructor called: 0xbfc23e2f
    Move Constructor called: 0xbfc23dff
    Move Constructor called: 0x9185dfc
    Destructor called: 0xbfc23dff
    Destructor called: 0xbfc23e2f
    Operator called: 0x9185dfc
    Destructor called: 0x9185dfc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an additional thread in some small testing app and want to
I have this thread. Earlier, I was testing the code without the thread and
I have created one testing app for running deep counter loop. I run the
I have created simple example with @Singleton, @Schedule and @Timeout annotations to try if
I have the following code for testing how to implement return values using pthread_exit()
I have been doing some testing with the following code to try and work
I have a vector of pointers to objects created with new . Multiple threads
I have a question about some code I'm testing to start understanding posix threads.
I am testing spawning off many threads running the same function on a 32
I've read a few threads on SO about usefulness of unit-testing various applications. The

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.