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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:55:01+00:00 2026-06-15T03:55:01+00:00

I am currently writing a multithreaded program where a thread may sometimes be created

  • 0

I am currently writing a multithreaded program where a thread may sometimes be created depending on certain circumstances. If this thread is created it needs to run independently of all other threads and I cannot afford to block any other threads to wait for it to join. The length of time the spawned thread runs for varies; sometimes it can take up to a few hours.

I have tried spawning the thread and putting a join in the destructor of the class which works fine, however if the code within the spawned thread finishes a long time before the destructor is called (which will be around 99% of the time) I would like the thread to kill itself freeing all its resources etc.

I looked into using detach for this, but you can’t rejoin a detached thread and on the off chance the destructor is called before this thread finishes then the spawned thread will not finish and could have disastrous consequences.

Is there any possible solution that ensures the thread finishes before the class is destructed as well as allowing it to join as soon as the thread finishes its work?

I am using boost/c++11 for threading. Any help at all would be greatly appreciated.

Thanks

  • 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-15T03:55:03+00:00Added an answer on June 15, 2026 at 3:55 am

    The thread may detach itself, releasing its resources. If the destructor sees that the thread is joinable, i.e. still running, let it join. If the thread reaches its end, self-detach. Possible race condition: is_joinable() returns true in destructor – thread detaches itself – destructor joins and fails miserably. So use a mutex guarding the thread’s decease:

    struct ThreadContainer
    {
       std::mutex threadEndMutex;
       std::thread theThread;
    
       ThreadContainer()
         : theThread([=]()
           {
             /* do stuff */
    
             // if the mutex is locked, the destructor is just
             // about to join, so we let him.
             if (threadEndMutex.try_lock())
               theThread.detach();
           })
       {}
    
       ~ThreadContainer()
       {
         // if the mutex is locked, the thread is just about 
         // to detach itself, so no need to join.
         // if we got the mutex but the thread is not joinable, 
         // it has detached itself already.
         if (threadEndMutex.try_lock() && theThread.is_joinable())
           theThread.join();
       }
    };
    

    PS:
    you might not even need the call to is_joinable, because if the thread detached itself, it never unlocked the mutex and try_lock fails.

    PPS:
    instead of the mutex, you may use std::atomic_flag:

    struct ThreadContainer
    {
       std::atmoic_flag threadEnded;
       std::thread theThread;
    
       ThreadContainer()
         : threadEnded(ATOMIC_FLAG_INIT)
         , theThread([=]()
           {
             /* do stuff */
    
             if (!threadEnded.test_and_set())
               theThread.detach();
           })
       {}
    
       ~ThreadContainer()
       {
         if (!threadEnded.test_and_set())
           theThread.join();
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently writing a c++ program that needs to extract string and numeric data
I'm currently writing a little Stata program in an .ado file. Within this .ado
I am currently writing a program using Weka that builds a model (using one
I'm currently trying to pass a mono threaded program to multithread. This software do
I'm currently writing program to encode and decode messages via error-correcting cyclic codes. My
I'm currently writing a small script using pygame, but I don't believe this question
So, I've got a multithreaded python program, which is currently suffering from deadlock. I
I'm writing a small program that uses a certain percentage of CPU. The basic
I'm currently writing a large multi threaded C++ program (> 50K LOC). As such
Currently writing a wifi positioning program and i need to scan the surrounding for

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.