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

The Archive Base Latest Questions

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

How can I wait for a detached thread to finish in C++? I don’t

  • 0

How can I wait for a detached thread to finish in C++?

I don’t care about an exit status, I just want to know whether or not the thread has finished.

I’m trying to provide a synchronous wrapper around an asynchronous thirdarty tool. The problem is a weird race condition crash involving a callback. The progression is:

  1. I call the thirdparty, and register a callback
  2. when the thirdparty finishes, it notifies me using the callback — in a detached thread I have no real control over.
  3. I want the thread from (1) to wait until (2) is called.

I want to wrap this in a mechanism that provides a blocking call. So far, I have:

class Wait {
  public:
  void callback() {
    pthread_mutex_lock(&m_mutex);
    m_done = true;
    pthread_cond_broadcast(&m_cond);
    pthread_mutex_unlock(&m_mutex);
  }

  void wait() {
    pthread_mutex_lock(&m_mutex);
    while (!m_done) {
      pthread_cond_wait(&m_cond, &m_mutex);
    }
    pthread_mutex_unlock(&m_mutex);
  }

  private:
  pthread_mutex_t m_mutex;
  pthread_cond_t  m_cond;
  bool            m_done;
};

// elsewhere...
Wait waiter;
thirdparty_utility(&waiter);
waiter.wait();

As far as I can tell, this should work, and it usually does, but sometimes it crashes. As far as I can determine from the corefile, my guess as to the problem is this:

  1. When the callback broadcasts the end of m_done, the wait thread wakes up
  2. The wait thread is now done here, and Wait is destroyed. All of Wait’s members are destroyed, including the mutex and cond.
  3. The callback thread tries to continue from the broadcast point, but is now using memory that’s been released, which results in memory corruption.
  4. When the callback thread tries to return (above the level of my poor callback method), the program crashes (usually with a SIGSEGV, but I’ve seen SIGILL a couple of times).

I’ve tried a lot of different mechanisms to try to fix this, but none of them solve the problem. I still see occasional crashes.

EDIT: More details:

This is part of a massively multithreaded application, so creating a static Wait isn’t practical.

I ran a test, creating Wait on the heap, and deliberately leaking the memory (i.e. the Wait objects are never deallocated), and that resulted in no crashes. So I’m sure it’s a problem of Wait being deallocated too soon.

I’ve also tried a test with a sleep(5) after the unlock in wait, and that also produced no crashes. I hate to rely on a kludge like that though.

EDIT: ThirdParty details:

I didn’t think this was relevant at first, but the more I think about it, the more I think it’s the real problem:

The thirdparty stuff I mentioned, and why I have no control over the thread: this is using CORBA.

So, it’s possible that CORBA is holding onto a reference to my object longer than intended.

  • 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-12T21:50:29+00:00Added an answer on May 12, 2026 at 9:50 pm

    Yes, I believe that what you’re describing is happening (race condition on deallocate). One quick way to fix this is to create a static instance of Wait, one that won’t get destroyed. This will work as long as you don’t need to have more than one waiter at the same time.

    You will also permanently use that memory, it will not deallocate. But it doesn’t look like that’s too bad.

    The main issue is that it’s hard to coordinate lifetimes of your thread communication constructs between threads: you will always need at least one leftover communication construct to communicate when it is safe to destroy (at least in languages without garbage collection, like C++).

    EDIT:
    See comments for some ideas about refcounting with a global mutex.

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

Sidebar

Ask A Question

Stats

  • Questions 286k
  • Answers 286k
  • 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 Include this wherever you would like that script to appear:… May 13, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer Just thinking quickly about this, I could see some kind… May 13, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer The Pragmatic Programmers popularized the DRY principle: Don't Repeat Yourself.… May 13, 2026 at 4:51 pm

Related Questions

How can I wait for a detached thread to finish in C++? I don't
I'm about to create a series of NSOperation s and run them in a
I have a windows forms application on which I need to use a for
I want to call WaitHandle.WaitOne(TimeSpan) in .NET, but I'm on the STA thread and
So, I have a Makefile which runs different commands of how to build S/W.

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.