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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:05+00:00 2026-06-04T04:37:05+00:00

This question comes from: C++11 thread doesn't work with virtual member function As suggested

  • 0

This question comes from:
C++11 thread doesn't work with virtual member function

As suggested in a comment, my question in previous post may not the right one to ask, so here is the original question:

I want to make a capturing system, which will query a few sources in a constant/dynamic frequency (varies by sources, say 10 times / sec), and pull data to each’s queues. while the sources are not fixed, they may add/remove during run time.

and there is a monitor which pulls from queues at a constant freq and display the data.

So what is the best design pattern or structure for this problem.

I’m trying to make a list for all the sources pullers, and each puller holds a thread, and a specified pulling function (somehow the pulling function may interact with the puller, say if the source is drain, it will ask to stop the pulling process on that thread.)

  • 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-04T04:37:06+00:00Added an answer on June 4, 2026 at 4:37 am

    Unless the operation where you query a source is blocking (or you have lots of them), you don’t need to use threads for this. We could start with a Producer which will work with either synchronous or asynchronous (threaded) dispatch:

    template <typename OutputType>
    class Producer
    {
        std::list<OutputType> output;
    
    protected:
        int poll_interval; // seconds? milliseconds?
        virtual OutputType query() = 0;
    
    public:
        virtual ~Producer();
    
        int next_poll_interval() const { return poll_interval; }
        void poll() { output.push_back(this->query()); }
    
        std::size_t size() { return output.size(); }
        // whatever accessors you need for the queue here:
        // pop_front, swap entire list, etc.
    };
    

    Now we can derive from this Producer and just implement the query method in each subtype. You can set poll_interval in the constructor and leave it alone, or change it on every call to query. There’s your general producer component, with no dependency on the dispatch mechanism.

    template <typename OutputType>
    class ThreadDispatcher
    {
        Producer<OutputType> *producer;
        bool shutdown;
        std::thread thread;
    
        static void loop(ThreadDispatcher *self)
        {
            Producer<OutputType> *producer = self->producer;
    
            while (!self->shutdown)
            {
                producer->poll();
                // some mechanism to pass the produced values back to the owner
                auto delay = // assume millis for sake of argument
                    std::chrono::milliseconds(producer->next_poll_interval());
                std::this_thread::sleep_for(delay);
            }
        }
    
    public:
        explicit ThreadDispatcher(Producer<OutputType> *p)
          : producer(p), shutdown(false), thread(loop, this)
        {
        }
    
        ~ThreadDispatcher()
        {
            shutdown = true;
            thread.join();
        }
    
        // again, the accessors you need for reading produced values go here
        // Producer::output isn't synchronised, so you can't expose it directly
        // to the calling thread
    };
    

    This is a quick sketch of a simple dispatcher that would run your producer in a thread, polling it however often you ask it to. Note that passing produced values back to the owner isn’t shown, because I don’t know how you want to access them.

    Also note I haven’t synchronized access to the shutdown flag – it should probably be atomic, but it might be implicitly synchronized by whatever you choose to do with the produced values.

    With this organization, it’d also be easy to write a synchronous dispatcher to query multiple producers in a single thread, for example from a select/poll loop, or using something like Boost.Asio and a deadline timer per producer.

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

Sidebar

Related Questions

Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
This may be a bit of daft question, but I don't come from an
This question has been puzzling me for a long time now. I come from
I come from Windows .Net forms development. This is a pretty basic/fundamental question. I'm
Okay, this question comes through a friend so it might be lost in translation...
( Late edit: This question will hopefully be obsolete when Java 7 comes, because
I recognize this may be a duplicate post, but I want to make sure
This question is related to following thread. Prism RegionAdapter - Removing then Adding View
This is a very conceptual question. Let's say I have 2 separate threads. Thread
This is a successor of my previous question, Is this variable being safely accessed

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.