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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:53:42+00:00 2026-05-28T01:53:42+00:00

I’m trying to implement a number of classes based on a a common class

  • 0

I’m trying to implement a number of classes based on a a common class that abstracts a thread-pool using boost.threadpool. I’ve got something that works (in Xcode on osx 10.7.2) but I’m really not sure its good design or if its even safe (largely because of what I’ve read on-line about the use of virtual member functions with templates). I’m looking for some style advice on the best way to implement something like this. I’m learning as I go along here so I know a lot of this will be ‘bad form’…

I have a base class called ‘workqueue’ like this:

template <typename T> 
class Workqueue{

    private:
        pool            *pThreadPool;

    public:
        Workqueue       (int);        
        void Start      (T);        
        void Schedule   (T);
        virtual bool Process(T) {return true;}
};

template <typename T> Workqueue<T>::Workqueue(int thread_count){
    pThreadPool = new pool(thread_count);
}

template <typename T> void Workqueue<T>::Start(T data){
    pThreadPool->schedule(boost::bind(&Workqueue::Process,this, data));  
    pThreadPool->wait();
}

template <typename T> void Workqueue<T>::Schedule(T data){
    pThreadPool->schedule(boost::bind(&Workqueue::Process,this, data));    
}

I then define a new service based on this class like this:

struct Service1Data{
    string item_data;
};

class MyService : public Workqueue<Service1Data> {    
public:
    MyService       (int);
    bool Process    (Service1Data);        
};

MyService::MyService(int workers) : Workqueue<Service1Data>(workers) {}

bool MyService::Process(Service1Data service_data){
    cout << "in process (" << service_data.item_data << ")" << endl;     
    return true;
}

(I’ve removed as much of the code to keep it simple so as shown would run forever as it continually submits new work). I use the service like this:

    MyService *service1 = new MyService(5);

    Service1Data x;
    x.item_data = "testing";
    service1->Start(x);

    // will wait until no more work.
    delete service1;

so my specific questions:

firstly (and please be gentle…) is this bad form and is there a much better way to do this? (and why?)

secondly – is this even safe given the virtual/template issues? I read somewhere that it should be safe if the class itself is templated and I think I understand the basic vtable issues – but really not sure of the specifics.

thirdly – the base workqueue class needs to have the member definitions in the ‘h’ file with the class definition for it to link. Not sure why that would be – I imagine it’s a linker issue to do with the virtual/template issues and so makes me nervous.

all help gratefully received..

Chris

  • 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-28T01:53:43+00:00Added an answer on May 28, 2026 at 1:53 am

    I think, you shouldn’t mix processing of data with processing of queue.

    I mean, you shouldn’t have Process method in your Workqueue. Data may process itself, or your queue can get processing function as (template?) parameter.

    Then you get rid of all your problems with virtual function. YourService class then should agregate Workqueue and may provide process function.

    Also, I doubt if you really need Workqueue. You can just use pThreadPool in YourService.

    If you need a common interface for services, you should specify it explicitly & separately. Your inheritance chain looks unclear. inheritance means is. Why YourService is Workqueue. I do not believe! I think YourService can use any sort of queue. But usage is aggregation.

    EDIT:
    Code will look like this:

    template<typename Processor>
    class WorkQueue
    {
    public:
        WorkQueue(int count, Processor& processor):_processor(processor),_pool(count) {}
    
        template <typename Data>
        void schedule(const Data& data)
        {
            _pool->schedule(std::bind(&Processor::process,_processor, data));
        }
    
        template <typename Data>
        void run(const Data& data)
        {
            schedule(data);
            _pool->wait();
        }
    
    private:
        Processor& _processor;
        pool _pool;
    };
    
    class Service
    {
    public:
        virtual void run() = 0;
        virtual ~Service() {}
    };
    
    struct ServiceParams
    {
        int param;
    };
    
    class MyService: public Service
    {
    
        friend class WorkQueue<MyService>;
    public:
        MyService(const ServiceParams& params): _params(params), _queue(1, *this) {}
        void run() { return _queue.run(_params); }
    private:
        ServiceParams _params;
        WorkQueue<MyService> _queue;
    
        void process(const ServiceParams& params) {std::cout <<"hello, world\n";}
    };
    

    EDIT: I originally considered usage as:

    ServiceData data;
    Service* service = new MyService(data);
    service->run();
    delete service;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.