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

The Archive Base Latest Questions

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

I have a boost::asio::io_service running in a thread that performs some operation: struct clicker

  • 0

I have a boost::asio::io_service running in a thread that performs some operation:

struct clicker
{
    clicker(boost::asio::io_service& io) : timer_(io) { wait_for_timer(); }

    void stop() { timer_.cancel(); }

    void wait_for_timer()
    {
        timer_.expires_from_now(std::chrono::milliseconds(500));
        timer_.async_wait(std::bind(&clicker::wait_completed, this, _1));
    }

    void wait_completed(const boost::system::error_code& err)
    {
        if (!err) {
            std::cout << "Click" << std::endl;
            wait_for_timer();
        }
    }

    boost::asio::steady_timer timer_;
};

int main()
{
    boost::asio::io_service io;
    clicker cl(io);

    std::thread io_thread(&boost::asio::io_service::run, &io);

    while (true) { // the run loop
        // gather input
        if (user_clicked_stop_button()) { cl.stop(); break; }
    }

    io_thread.join();
}

Now calling stop() should cancel waiting for the timer and fire wait_completed() with an error. However, we have a race condition here: at times, stop() will be called while wait_for_timer() is running and before the async_wait has been scheduled. Then, the code will run indefinitely.

What’s the recommended way to deal with this situation? A boolean flag inside clicker that is tested in wait_completed? A mutex?

Update:

This is just a simplified example, in the real code I have several operations running in the io_service, so calling io_service::stop() is not an option here.

  • 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-12T09:04:32+00:00Added an answer on June 12, 2026 at 9:04 am

    Post the action to the io_service thread:

    void stop() 
    { 
      timer_.get_io_service().post([=] { timer_.cancel(); }); 
    } 
    

    (If your compiler is not c++11-compatible, use bind to create the lambda.)

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

Sidebar

Related Questions

I have the following thread pool. #include <queue> #include <map> #include <boost/shared_ptr.hpp> #include <boost/asio/io_service.hpp>
I have an ip::udp::socket constructed with an io_service . There is only one boost::thread
I have a C++ client app that uses Boost ASIO to make SSL connections
I have many different threads ( boost::thread_pool ) to a boost::asio::io_service . The io_service
Greetings. I'm just getting started with the boost::asio library and have run into some
I have a boost::asio based server which works fine, except that I'm trying to
I have the following minmal example of a thread pool made with boost::asio. #include
Is there any way I can have thread-safe coroutines along with boost::asio? I want
I have a class, which has a boost::asio::io_service object. I want this object stored
My code uses boost::asio and io_service in a single thread to perform various socket

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.