Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The standard async approach fails here (the send callbacks may block other callbacks while waiting for a new object to send)</p> </blockquote> <p>The async model should work fine, and if used properly is definitely going to scale better&mdash;it only falls apart when you introduce blocking. Instead of ditching async, here's what I would suggest:</p> <p>Remove the condition variable. You need two queues: OUTPUT and WAITING.</p> <p>Then when processing a client:</p> <ol> <li>If there's data in the OUTPUT queue, send it.</li> <li>If not, push it on the WAITING queue.</li> </ol> <p>There is no requirement to perform the next I/O from within the handler of a previous one. Here instead of blocking on a condition variable, we just offhand it onto the WAITING queue for later processing.</p> <p>And in the OUTPUT-pushing code:</p> <ol> <li>If there are clients in the WAITING queue, send the data directly.</li> <li>If not, push onto the OUTPUT queue.</li> </ol> <p>Here's some pseudo-code:</p> <pre><code>queue&lt;packet&gt; output; queue&lt;client&gt; waiting; void try_send(client c) { if(!output.empty()) { // there is output waiting to be sent, send it. packet p = output.pop(); c.async_send(p, on_send_finished); } else { // nothing available, go back to waiting. waiting.push(c); } } void on_send_finished(client c) { // send finished, try again if any more output has accumulated: try_send(c); } void push_output(packet p) { output.push(p); if(!waiting.empty()) { // there is a client waiting to send, give it a try. client c = waiting.pop(); try_send(c); } } </code></pre> <p>This can all be done in a scalable way using a single thread, but multiple threads are pretty easy with asio. If you're going to use multiple threads, you'll want to introduce a lock in the logic that checks the queues.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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