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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:54:52+00:00 2026-05-29T06:54:52+00:00

I am currently developing a basic thread pool. I used c++11’s std::thread , along

  • 0

I am currently developing a basic thread pool. I used c++11’s std::thread, along with std::condition_variable, and std::unique_lock. It seems to work, and I now would like to be able to kill some threads when too many of them are inactive.
For now, their jobs are given through a std::queue of boost::functions. I was thinking of adding a bunch of empty boost::functions so the threads know they have to exit their loop.
The thread’s loop is like this:

void    ThreadPool::threadLoop()
{
  boost::function<void ()>      oThreadTask;
  std::unique_lock<std::mutex>  oLock(m_oTaskMutex);

  while (1)
    {
      // m_oCV is a static std::condition_variable
      // m_oTaskQueue is a static std::queue< boost::function<void ()> >
      m_oCV.wait(oLock, [](){ return m_oTaskQueue.size(); });
      oThreadTask = m_oTaskQueue.front();
      m_oTaskQueue.pop();
      m_oTaskMutex.unlock();
      if (oThreadTask.empty())
        break ;
      oThreadTask();
    }
  // ??
}

The thing is, I’m not sure how to properly detach the thread once I exited the loop.
Would it be clean to look for the thread’s handle (I have access to a std::list<std::thread*>, and can compare their ids with std::this_thread::get_id()), and is it safe to call detach() from the thread itself, or even join()?

  • 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-29T06:54:53+00:00Added an answer on May 29, 2026 at 6:54 am

    Yes, if you have all thread objects stored, you can find the one which thread::id is equal to this_thread::get_id(), and you can call detach() on it, and destroy the thread object after that (the C++11 standard does not prevent that, and I believe it is based on the common practice). Make sure that no other execution thread accesses the instance of std::thread which is destroyed.

    But you cannot call join() from the thread itself: an attempt for a thread to join with itself would result in a deadlock, and C++11 implementations should recognize that and throw system_error with the error condition of resource_deadlock_would_occur.

    Alternatively, you can leave a message (e.g. via an std::atomic variable) to the main thread that the thread associated with a particular instance of std::thread is about to complete its execution, and let the main thread join with this instance at a later point.

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

Sidebar

Related Questions

I'm fairly new to Cocoa and Objective-C. Currently I'm developing a fairly basic application
Alright, here we go! I am currently developing an iPhone app that will work
I'm currently developing the basic version of an iOS app. At some point in
I'm currently developing a very simple app with very basic syntax highlighting in a
i'm new to Cocoa, maybe these questions are very basic. Currently I'm developing a
Currently in the ASP.NET application I'm developing, basic validations (ie required fields) are being
I'm developing this app which as a very basic Ajax form and I'm currently
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Currently developing a PHP framework and have ran into my first problem. I need
We are currently developing a server whereby a client requests interest in changes to

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.