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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:39:23+00:00 2026-05-26T12:39:23+00:00

I found a threadpool which doesn’t seem to be in boost yet, but I

  • 0

I found a threadpool which doesn’t seem to be in boost yet, but I may be able to use it for now (unless there is a better solution).

I have several million small tasks that I want to execute concurrently and I wanted to use a threadpool to schedule the execution of the tasks. The documentation of the threadpool provides (roughly) this example:

#include "threadpool.hpp"
using namespace boost::threadpool;

// A short task
void task()
{
    // do some work
}

void execute_with_threadpool(int poolSize, int numTasks)
{
    // Create a thread pool.
    pool tp(poolSize);

    for(int i = 0; i++; i < numTasks)
    {
        // Add some tasks to the pool.
        tp.schedule(&task);
    }
    // Leave this function and wait until all tasks are finished.
}

However, the example only allows me to schedule non-member functions (or tasks). Is there a way that I can schedule a member function for execution?

Update:

OK, supposedly the library allows you to schedule a Runnable for execution, but I can’t figure out where is the Runnable class that I’m supposed to inherit from.

template<typename Pool, typename Runnable>
bool schedule(Pool& pool, shared_ptr<Runnable> const & obj);

Update2:

I think I found out what I need to do: I have to make a runnable which will take any parameters that would be necessary (including a reference to the object that has a function which will be called), then I use the static schedule function to schedule the runnable on the given threadpool:

class Runnable
{
private:
    MyClass* _target;
    Data* _data;
public:
    Runnable(MyClass* target, Data* data)
    {
        _target = target;
        _data = data;
    }

    ~Runnable(){}

    void run()
    {
        _target->doWork(_data);
    }
};

Here is how I schedule it within MyClass:

void MyClass::doWork(Data* data)
{
    // do the work
}

void MyClass::produce()
{
    boost::threadpool::schedule(myThreadPool, boost::shared_ptr<Runnable>(new Runnable(myTarget, new Data())));
}

However, the adaptor from the library has a bug in it:

template<typename Pool, typename Runnable>
bool schedule(Pool& pool, shared_ptr<Runnable> const & obj)
{ 
    return pool->schedule(bind(&Runnable::run, obj));
} 

Note that it takes a reference to a Pool but it tries to call it as if it was a pointer to a Pool, so I had to fix that too (just changing the -> to a .).

  • 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-26T12:39:24+00:00Added an answer on May 26, 2026 at 12:39 pm

    I think I found out what I need to do: I have to make a runnable which will take any parameters that would be necessary (including a reference to the object that has a function which will be called), then I use the static schedule function to schedule the runnable on the given threadpool:

    class Runnable
    {
    private:
        MyClass* _target;
        Data* _data;
    public:
        Runnable(MyClass* target, Data* data)
        {
            _target = target;
            _data = data;
        }
    
        ~Runnable(){}
    
        void run()
        {
            _target->doWork(_data);
        }
    };
    

    Here is how I schedule it within MyClass:

    void MyClass::doWork(Data* data)
    {
        // do the work
    }
    
    void MyClass::produce()
    {
        boost::threadpool::schedule(myThreadPool, boost::shared_ptr<Runnable>(new Runnable(myTarget, new Data())));
    }
    

    However, the adaptor from the library has a bug in it:

    template<typename Pool, typename Runnable>
    bool schedule(Pool& pool, shared_ptr<Runnable> const & obj)
    { 
        return pool->schedule(bind(&Runnable::run, obj));
    } 
    

    Note that it takes a reference to a Pool but it tries to call it as if it was a pointer to a Pool, so I had to fix that too (just changing the -> to a .).

    However, as it turns out, I can’t use that boost thread pool because I am mixing native C++ (dll), C++/CLI (dll) and .NET code: I have a C++/CLI library that wraps a native C++ library which in tern uses boost::thread. Unfortunately, that results in a BadImageFormatException at runtime (which has previously been discussed by other people):

    The problem is that the static boost thread library tries to hook the
    native win32 PE TLS callbacks in order to ensure that the thread-local
    data used by boost thread is cleaned up correctly. This is not
    compatible with a C++/CLI executable.

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

Sidebar

Related Questions

Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
I found this open-source library that I want to use in my Java application.
I found What are mvp and mvc and what is the difference but it
I can;t seem to find an easy way to determine when a ThreadPool has
I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool
I found there are two ways (submit and execute) to add a Runnable into
Looking for some sample code (C#) for a simple thread pool implementation. I found
Found the following in an Oracle-based application that we're migrating (generalized) : SELECT Table1.Category1,
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can

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.