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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:21:48+00:00 2026-05-21T17:21:48+00:00

I’m experimenting with Boost.Asio strand for a server I’m writing and wanted to clarify

  • 0

I’m experimenting with Boost.Asio strand for a server I’m writing and wanted to clarify a few things.

Let’s assume we have SomeClass with something like this:

void SomeClass::foo()
{
    strand_.dispatch(boost::bind(&SomeClass::bar, this));
}

Further, the strand has an io_service with multiple threads calling run().

In the documentation on strand::dispatch() I read it guarantees that handlers posted or dispatched through the strand will not execute concurrently and if this is met the handler may execute in this function. What decides if the handler gets to be executed immediately?

In this case with multiple threads, will dispatch block if multiple io_service threads call SomeClass::foo concurrently? I.e. block for all but the one that gets executed. If so, is the handlers executed in order (the order they called dispatch())?

  • 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-21T17:21:49+00:00Added an answer on May 21, 2026 at 5:21 pm

    Logically, it would be impossible for this guarantee to be met without some possibility of (b)locking. The code for dispatch (in detail/impl/strand_service.hpp from 1.46.1) confirms this. That’s the nice thing about Boost, it’s all there for you to see. There is info on order of handler invocation in the docs here, including a comment that sometimes no ordering guarantee is provided.

    template <typename Handler>
    void strand_service::dispatch(strand_service::implementation_type& impl,
        Handler handler)
    {
      // If we are already in the strand then the handler can run immediately.
      if (call_stack<strand_impl>::contains(impl))
      {
        boost::asio::detail::fenced_block b;
        boost_asio_handler_invoke_helpers::invoke(handler, handler);
        return;
      }
    
      // Allocate and construct an operation to wrap the handler.
      typedef completion_handler<Handler> op;
      typename op::ptr p = { boost::addressof(handler),
        boost_asio_handler_alloc_helpers::allocate(
          sizeof(op), handler), 0 };
      p.p = new (p.v) op(handler);
    
      // If we are running inside the io_service, and no other handler is queued
      // or running, then the handler can run immediately.
      bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
      impl->mutex_.lock();
      bool first = (++impl->count_ == 1);
      if (can_dispatch && first)
      {
        // Immediate invocation is allowed.
        impl->mutex_.unlock();
    
        // Memory must be releaesed before any upcall is made.
        p.reset();
    
        // Indicate that this strand is executing on the current thread.
        call_stack<strand_impl>::context ctx(impl);
    
        // Ensure the next handler, if any, is scheduled on block exit.
        on_dispatch_exit on_exit = { &io_service_, impl };
        (void)on_exit;
    
        boost::asio::detail::fenced_block b;
        boost_asio_handler_invoke_helpers::invoke(handler, handler);
        return;
      }
    
      // Immediate invocation is not allowed, so enqueue for later.
      impl->queue_.push(p.p);
      impl->mutex_.unlock();
      p.v = p.p = 0;
    
      // The first handler to be enqueued is responsible for scheduling the
      // strand.
      if (first)
        io_service_.post_immediate_completion(impl);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.