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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:18:16+00:00 2026-06-03T03:18:16+00:00

Assuming there is a std::deque queue of pointers to tasks to be performed, what

  • 0

Assuming there is a std::deque queue of pointers to tasks to be performed, what is the best way to ensure the number of threads running at one time is limited to the number of CPU cores? i.e. After a task is completed remaining task are subsequently launched.

I made the following code for an earlier post, the problem I have now is that I am unsure of the best strategy to implement what I have described, and thought it may be worth canvasing opinion.

Note that the “std::deque queue of pointers” I mentioned above is NOT referring to deque mtasks in the code. I do not wish to pop tasks from this deque as I’m using this to store previously completed tasks.

In the program type something like, task p1 p2 p3 p4 p5 , at the prompt and then info to check the current status of each. Note that currently all 5 tasks complete around the same time. However, what I actually want is the first 2 to complete (dual core machine) then the next 2 etc.

The reason for this is that in practice these tasks may take hours, and therefore I want to get the first lot of results sooner so that I may load them into MATLAB or whatever.

I hope I made myself and the example code clear. I guess thread::hardware_concurrency() covers the number of cores part….

Thanks
A.

#include <iostream>  
#include <string>
#include <sstream>
#include <boost/thread.hpp>  

using namespace std;

class task {
public:
    string mname;
    bool completed;
    void start()
    {
        int a = 0;
        for (int i=0 ; i<10000; i++)
        {
            for (int j=0 ; j<100000; j++)
            {
                a= i*2;
            }
        }
        this->completed = true;
    }
    task(string name)
    {
        mname = name;
        completed = false; 
    }
};

class taskManager{
    public:
        boost::thread_group threads;
        void startTask( string name )
        {
            //add new task to vector list           
            mtasks.push_back( task(name) );
            // execute start() on a new thread
            threads.create_thread( boost::bind( &task::start, &mtasks.back()) );
        }
        int tasksTotal()
        {
            return mtasks.size();
        }
        string taskInfo(int i)
        {
            string compstr("Not Completed");
            if ( mtasks.at(i).completed == true )
            {
                compstr = "Completed";
            }
            return mtasks.at(i).mname + " " + compstr; 
        }
    private:
        deque<task> mtasks; 
};

int main(int argc, char* argv[])  
{
    string cmd, temp;
    stringstream os;
    bool quit = false;
    taskManager mm;

    cout << "PROMPT>";

    while (quit == false)
    {
        //Wait for a valid command from user
        getline(cin,cmd);

        // Reset stringstream and assign new cmd string
        os.clear(); 
        os << "";
        os << cmd;
        //parse input string
        while (os >> temp) 
        {               
            if ( temp.compare("task") == 0 )
            {
                while (os >> temp) { mm.startTask( temp ); }                     
            }
            if ( temp.compare("info") == 0 )
            { 
                // Returns a list of all completed and not completed tasks
                for (int i = 0; i<mm.tasksTotal(); i++)
                {
                    cout << mm.taskInfo(i).c_str() << endl;
                }                           
            }
            if ( temp.compare("quit") == 0 ){ quit = true; }
        }

        cout << "PROMPT>";
    }

    mm.threads.join_all();      

    return 0;  
};
  • 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-03T03:18:18+00:00Added an answer on June 3, 2026 at 3:18 am

    What you’re describing is the thread pool pattern, where you have a fixed number of threads and a group of tasks you want to perform using those threads (# tasks > # threads).

    The wikipedia entry on the thread pool pattern has more information. You can write your own, or use something like the unofficial boost threadpool library.

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

Sidebar

Related Questions

I'm currently estimating a new project. My high level estimate assuming there was one
Assuming that I know there is a git-daemon running at git://git.mycompany.com , how can
Assuming there's only one package in a file, does the order or the following
Writing some test cases and my mind wanders, assuming there is a better way
Is there a standard way to check whether construction of a new std::codecvt_byname succeeded?
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
Assuming there are 5 inputs in web form <input name='the_same[]' value='different' /> <input name='the_same[]'
Assuming there are buttons with all the letters from the alphabet set as Text.
Assuming there is a task (e.g. an image processing method with a lot math)
Should I be separating my js for each page assuming there is no overlap

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.