Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple callback control using boost::asio and c++11 lambda
    primarykey
    data
    text
    <p>I'm implementing simple server with <code>boost::asio</code> and thinking of io-service-per-cpu model(each io_service has one thread).</p> <p>What i want to do is, let an io_service to request some jobs to another io_service( something like message passing ).</p> <p>I think <code>boost::asio::io_service::post</code> can help me.</p> <p>There are two <code>io_service</code>s, <code>ios1</code>,<code>ios2</code>, </p> <p>and a job(function) <code>bool func(arg *)</code>,</p> <p>and a completion handler <code>void callback(bool)</code>.</p> <p>So I want <code>ios1</code> to request a job, <code>ios2</code> runs it and notify <code>ios1</code> to finish and finally <code>ios2</code> runs the handler.</p> <pre><code>ios2.post( [&amp;ios1, arg_ptr, callback, func] { bool result = func(arg_ptr); ios1.post( []{ callback(result) } ); } ); </code></pre> <p>Is this code works? and is there any smarter and simpler way?</p> <p><strong>EDIT:</strong></p> <p>I found that the second lamda inside the <code>ios1.post()</code> can't reach the function pointer <code>callback</code>. It's out of the scope... so I'm trying another way using <code>boost::bind()</code>.</p> <pre><code>ios2.post( [&amp;ios1, arg_ptr, callback, func] { ios1.post( boost::bind( callback, func(arg_ptr) ) ); } ); </code></pre> <p>I removed one stack variable bool and it seems better.</p> <p>But using c++11 lambda and <code>boost::bind</code> together doesn't look so cool.</p> <p>How can i do this without <code>boost::bind</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload