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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:18:05+00:00 2026-06-01T16:18:05+00:00

To make long story short, my code: #include <iostream> #include <map> #include <boost/asio.hpp> #include

  • 0

To make long story short, my code:

#include <iostream>
#include <map>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

namespace ba = boost::asio;
namespace bp = boost::posix_time;

typedef std::map<int, ba::deadline_timer*> timer_map;

timer_map g_timers;
boost::mutex g_timers_lock;

ba::io_service g_ios;

void on_timer(int id) {
    {
            boost::mutex::scoped_lock lock(g_timers_lock);
            timer_map::iterator it = g_timers.find(id);
            assert(it != g_timers.end());
            g_timers.erase(id);
    }

    // std::cout << "delete timer " << id << std::endl;
}

int main(void) {
    boost::thread trd(boost::bind(&ba::io_service::run, &g_ios));
    trd.detach();

    int count = 0;
    for (;;) {
            for (int i = 0; i < 100; i++) {
                    ba::deadline_timer* pt = new ba::deadline_timer(g_ios, bp::seconds(1));
                    pt->async_wait(boost::bind(&on_timer, count));

                    boost::mutex::scoped_lock lock(g_timers_lock);
                    g_timers.insert(std::make_pair(count++, pt));
            }

            usleep(20000);
    }

    return 0;
}

==================================

I know, I should lock the g_timers, but should I lock the g_ios?
I mean these lines:

ba::deadline_timer* pt = new ba::deadline_timer(g_ios, bp::seconds(1));
pt->async_wait(boost::bind(&on_timer, count));

Are thread safety? It reference the g_ios, and will it call g_ios.add_job(this_timer) ..etc.. ?

  • 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-01T16:18:06+00:00Added an answer on June 1, 2026 at 4:18 pm

    to directly answer your question, yes an instance of an io_service is thread safe. This is described in the documentation.

    Thread Safety

    Distinct objects: Safe.

    Shared objects: Safe, with the specific exceptions of the reset() and
    notify_fork() functions. Calling reset() while there are unfinished
    run(), run_one(), poll() or poll_one() calls results in undefined
    behaviour. The notify_fork() function should not be called while any
    io_service function, or any function on an I/O object that is
    associated with the io_service, is being called in another thread.

    Though, it’s not obvious to me what you’re attempting to accomplish. As written, your example accomplishes nothing because the io_service has no work to do when you invoke io_service::run() so it returns immediately. You ignored the return value, I suspect if you inspected it, it would be zero.

    Your use of mutexes is also questionable. In general, if you need access to a shared resource from within an asynchronous handler, prefer to use a strand instead of a mutex. This concept is discussed quite well in the Asio examples and documentation, as is the use of threads.

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

Sidebar

Related Questions

So to make a long story short. A professor asked this question in class
Short story: I can't make precompiled headers work properly with gcc -c option. Long
Okay so to make a long story short I am using this script in
To make a long story short, I'm trying to pass a list of dictionaries
To make a long story short, I'm having trouble getting a couple of Java
To make a long story short I have to use processing in a class
I'll try to make a long story short: I want to call a method
To make a long story short, my terminal was not working so a forum
I'm trying to make a WCF service that will work with JSON-P (long-story short,
It's a rather long story but to make it short I need to store

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.