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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:49:39+00:00 2026-06-10T22:49:39+00:00

I am writing a thread based application in C++. The following is sample code

  • 0

I am writing a thread based application in C++. The following is sample code showing how I am checking the thread count. I need to ensure that at any point in time, there are only 20 worker threads spawned from my application:

#include<stdio.h>
using namespace std;
class ThreadWorkerClass
{
  private:
    static int threadCount;
  public:
    void ThreadWorkerClass()
    {
      threadCount ++;
    }
    static int getThreadCount()
    {
      return threadCount;
    }
    void run()
    {
      /* The worker thread execution
       * logic is to be written here */
      //Reduce count by 1 as worker thread would finish here
      threadCount --;
    }
}

int main()
{
  while(1)
  {
    ThreadWorkerClass twObj;
    //Use Boost to start Worker Thread
    //Assume max 20 worker threads need to be spawned
    if(ThreadWorkerClass::getThreadCount() <= 20) 
      boost::thread *wrkrThread = new boost::thread(
        &ThreadWorkerClass::run,&twObj);
    else
      break;
  }
  //Wait for the threads to join
  //Something like (*wrkrThread).join();
  return 0;
}

Will this design require me to take a lock on the variable threadCount? Assume that I will be running this code in a multi-processor environment.

  • 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-10T22:49:41+00:00Added an answer on June 10, 2026 at 10:49 pm

    The design is not good enough. The problem is that you exposed the constructor, so whether you like it or not, people will be able to create as many instances of your object as they want. You should do some sort of threads pooling. i.e. You have a class maintaining a set of pools and it gives out threads if available. something like

    class MyThreadClass {
       public:
          release(){
            //the method obtaining that thread is reponsible for returning it
          }
    };
    
    class ThreadPool {
      //create 20 instances of your Threadclass
      public:
      //This is a blocking function
      MyThreadClass getInstance() {
         //if a thread from the pool is free give it, else wait
      }
    };
    

    So everything is maintaned internally by the pooling class. Never give control over that class to the others. you can also add query functions to the pooling class, like hasFreeThreads(), numFreeThreads() etc…

    You can also enhance this design through giving out smart pointer so you can follow how many people are still owning the thread.
    Making the people obtaining the thread responsible for releasing it is sometimes dangerous, as processes crashes and they never give the tread back, there are many solutions to that, the simplest one is to maintain a clock on each thread, when time runs out the thread is taken back by force.

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

Sidebar

Related Questions

I am writing code that will spawn two thread and then wait for them
I am writing event based android application in which I start main thread which
I have a UDP based application that is implemented using a thread pool. Messages
I'm writing an MPI program (Visual Studio 2k8 + MSMPI) that uses Boost::thread to
I am writing multi-threaded python application. The main thread create a Thread pool of
A part of an application I'm working on is a simple pthread-based server that
I'm looking for some advice on writing some thread safe, optimized, elegant code to
I'm developing a distributed application, and I need to connect a client Java based
I am writing an application based on a COM object from a third party
I am writing an MDI application in C# that contains two child forms which

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.