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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:20:31+00:00 2026-06-10T09:20:31+00:00

I have the following thread pool. #include <queue> #include <map> #include <boost/shared_ptr.hpp> #include <boost/asio/io_service.hpp>

  • 0

I have the following thread pool.

#include <queue>
#include <map>

#include <boost/shared_ptr.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/thread/thread.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> // remove me (only for io)

class ThreadPool
{
    void run() {
        // get some work from a task queue and then work on it
    }
public:
    void work_as_mainthread(void) { m_io_service.run(); }

    ThreadPool(int poolSize = 4) : timer(m_io_service)
    {
        timer.expires_from_now(boost::posix_time::seconds(1));
        m_pWork.reset( new boost::asio::io_service::work(m_io_service) );

        for ( int i = 0; i < poolSize; ++i)
            m_threadGroup.create_thread( boost::bind(&boost::asio::io_service::run, &m_io_service) );
    }

private:
    boost::asio::io_service m_io_service;
    boost::asio::deadline_timer timer;
    boost::shared_ptr<boost::asio::io_service::work> m_pWork;
    boost::thread_group m_threadGroup;
};

int main()
{
    int n_threads = 2;
    ThreadPool pool(n_threads);
    // add some tasks here...
    pool.work_as_mainthread();
    return 0;
}

It is a minimal exaple, full code here. Compile like this:

g++ -Wall -g -lboost_thread -lboost_date_time -lboost_system main.cpp -o main

Note that I have no async_wait() called (I don’t know why I should need it, the thread pool works so far).

Now, it might happen that suddenly one task wants another task to be done before the other task’s timeout. What’s the best way to tell the m_io_service to execute the handler immediatelly (even if the timer is not interested yet), and then to continue as if nothing happened? I could not figure out from the sparse documentation, really.

This did not work:

timer.expires_at(boost::posix_time::microsec_clock::universal_time());
  • 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-10T09:20:32+00:00Added an answer on June 10, 2026 at 9:20 am

    Note that expires_at only sets dealine_timer expiration time, but doesn’t “enable” the timer, so there’s no asynchronous operation associated with the timer. You should call async_wait passing it a completion handler: when the timeout is expired or the timer is cancelled, this handler gets called in one of the associated io_service‘s threads.

    Calling io_service::post is unrelated to deadline_timer. It just posts your functor to io_service queue, and io_service invokes it as soon as it can in one of its threads.

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

Sidebar

Related Questions

I have the following minmal example of a thread pool made with boost::asio. #include
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
I have the following thread code which executes correct first time. After that from
I have the following thread: class LemonadeMainMenuThread extends Thread { private SurfaceHolder mSurfaceHolder; private
I have the same problem that is listed in the following thread. WSDL first
I have the following error Exception in thread main javax.naming.NameNotFoundException: CounterBean not bound trying
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
I have the following function call from a thread: Thread Move = new Thread(){
I have the following code in a Runnable that gets passed to a thread
I have the following code: public void disconnect() { running = false; if(thread !=

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.