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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:02:16+00:00 2026-05-30T13:02:16+00:00

( This question , though similar, didn’t really answer my question.) I’ve had problems

  • 0

(This question, though similar, didn’t really answer my question.)

I’ve had problems with my own "thread group" implementation, and being no closer to solving or even identifying the issue, I’m looking into just using boost::thread_grp.

Now, from what documentation I can find on the subject1, I’ve always believed that thread objects — no matter the duration of their actual work — remain alive and a part of a thread group until the thread group is destroyed.

However, a cursory test seems to indicate that boost::thread_group::size() decreases on its own as threads do their work and terminate. That would imply that the thread objects themselves are being cleaned up for me, too.

Is this true? Can I rely on it?

#include <cassert>
#include <unistd.h>         // for sleep()
#include <boost/thread.hpp>

boost::mutex m;
unsigned int count = 0;

void func() {
   boost::mutex::scoped_lock l(m);
   count++;
}

int main()
{
   boost::thread_group grp;
   for (size_t i = 0; i < 300; i++)
      grp.create_thread(func());
   
   sleep(10);
   
   assert(count == 300);
   assert(grp.size() == 0);  // passes, in my tests
   
   // ^ Can I rely on that?
   //   Do I really have no thread objects eating up
   //     memory, at this point?
   
   grp.join_all();
   // ^ Then I guess this is doing nothing, in this case...
}

If it is, then my entire original has_threads implementation was just a broken clone and I’ve wasted my time. 🙂

And, yes, assert was a really poor choice for this snippet, since I guess it could lead to any unlikely-outstanding threads from butchering the memory under what used to be count? Anyway, never mind that.


1 – I’m stuck on Boost 1.40, but the documentation on the matter seems to be the same for more recent versions.


Update

This thread is an example of where people are saying the opposite of what my testing has shown. It does also provide a decent solution if that’s the case, though; it’s nice that thread_group is totally thread-safe (though I’m not convinced that that shared_ptr::get() is thread-safe; what if the thread ends before the shared_ptr::reset has finished?). Do I need to employ this solution?

  • 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-30T13:02:18+00:00Added an answer on May 30, 2026 at 1:02 pm

    No, I misinterpreted the results of my test by not abstracting away the task scheduler that creates threads for me. In short, I was checking size() on the wrong boost::thread_group.

    In fact, boost::thread_group::size() doesn’t go down on its own as threads terminate, so I’ll be using an adapted version of the solution provided in the forum post I linked to in the question update.

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

Sidebar

Related Questions

I didn't see any similar questions asked on this topic, and I had to
This is similar to this question , but I thought I'd reword it a
This may be a simple question though can´t figure out how to do it.
This question is related to this one , though I think I was a
I feel as though this this is a simple question, but can't find an
I've gone through this SO question but it didn't help. The case here is
I've already asked question similar to this some time ago, I thought I solved
I have asked a similar question before, but didn't get very good results. I've
Similar to this question , but my setup is a little different. I've written
I have asked a similar question before, but I didn't have a firm grasp

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.