Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Correct handler signature for io_service::post/dispatch
From: Lars Viklund (zao_at_[hidden])
Date: 2012-05-20 12:09:07


On Sun, May 20, 2012 at 04:52:41PM +0100, Gary Sanders wrote:
> On 20/05/2012 16:08, Igor R wrote:
>>> Currently I'm developing a server that uses Boost.Asio, version 1.49. It
>>> makes use of io_service::post and io_service::dispatch to invoke handlers
>>> running in the same process, but on different threads. Some of the handlers
>>> invoke a function with the following signature:
>>>
>>> void handler(std::string s);
>>>
>>> This works fine and no problems have been observed. However, subsequently I
>>> read in the asio documentation that the function signature of a handler
>>> invoked by post/dispatch *must* be in the following format:
>>>
>>> void handler();
>>
>>
>> io_service::post() accepts nullary functors. So if your handler accept
>> parameters, I guess you bind them at when posting the handler, don't
>> you.
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> Yes, this is the sort of thing that I'm doing and that I find to be
> working OK.
>
> ios.post(boost::bind(&MyClass::Handler,ptr_to_my_class,"astring"));
>
> So, this is OK?

The result of the bind call is an object of a type that overloads
operator () with suitable parameters. Unless the recipient is aware of
the undocumented return type of Bind, all it sees is a callable object.

As Igor said, post accepts anything that is callable with the signature
"void ()". This includes the result of your bind expression, a free
function pointer with no parameters, an user-defined class with
overloaded nullary operator(), etc.

In this case, you have no placeholders, so it's invokable with any set
of parameters (unused arguments are ignored), so it satisfies the
requirements that post() have.

If you tried to do something like this, you'd fail miserably as you do
not honor the contract on the parameter for Post:

  void f(std::string s);
  ios.post(&f);

-- 
Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net