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

  • SEARCH
  • Home
  • 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 376699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:37:29+00:00 2026-05-12T14:37:29+00:00

I am working on a multithreaded program using C++ and Boost. I am using

  • 0

I am working on a multithreaded program using C++ and Boost. I am using a helper thread to eagerly initialize a resource asynchronously. If I detach the thread and all references to the thread go out of scope, have I leaked any resources? Or does the thread clean-up after itself (i.e. it’s stack and any other system resources needed for the itself)?

From what I can see in the docs (and what I recall from pthreads 8 years ago), there’s not explicit “destory thread” call that needs to be made.

I would like the thread to execute asynchronously and when it comes time to use the resource, I will check if an error has occured. The rough bit of code would look something like:

//Assume this won't get called frequently enough that next_resource won't get promoted
//before the thread finishes.
PromoteResource() {
   current_resource_ptr = next_resource_ptr;
   next_resource_ptr.reset(new Resource());
   callable = bind(Resource::Initialize, next_resource); //not correct syntax, but I hope it's clear
   boost::thread t(callable);
   t.start();
}

Of course–I understand that normal memory-handling problems still exist (forget to delete, bad exception handling, etc)… I just need confirmation that the thread itself isn’t a “leak”.

Edit: A point of clarification, I want to make sure this isn’t technically a leak:

void Run() {
   sleep(10 seconds);
}

void DoSomething(...) {
   thread t(Run);
   t.run();
} //thread detaches, will clean itself up--the thread itself isn't a 'leak'?

I’m fairly certain everything is cleaned up after 10 seconds-ish, but I want to be absolutely certain.

  • 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-12T14:37:29+00:00Added an answer on May 12, 2026 at 2:37 pm

    The thread’s stack gets cleaned up when it exits, but not anything else. This means that anything it allocated on the heap or anywhere else (in pre-existing data structures, for example) will get left when it quits.

    Additionally any OS-level objects (file handle, socket etc) will be left lying around (unless you’re using a wrapper object which closes them in its destructor).

    But programs which frequently create / destroy threads should probably mostly free everything that they allocate in the same thread as it’s the only way of keeping the programmer sane.

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

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you using a tool like fiddler to see what… May 12, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer use array_unique($array) in PHP you can also iterate over array… May 12, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer Django doesn't currently support multiple databases. However you can just… May 12, 2026 at 7:21 pm

Related Questions

Do you have any advices how to test a multithreaded application? I know, threading
I wrote a multi-threaded program which does some CPU heavy computation with a lot
I am new to programming in general so please keep that in mind when
I'm learning about POSIX threads right now, but I guess this is just a
I am currently cleaning up a table with 2 indexes and 250 million active

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.