VLink  2.0.0
A high-performance communication middleware
multi_loop.h File Reference

Multi-threaded variant of MessageLoop that forwards tasks to an internal ThreadPool. More...

#include <memory>
#include "./message_loop.h"
Include dependency graph for multi_loop.h:

Go to the source code of this file.

Classes

class  vlink::MultiLoop
 MessageLoop subclass that forwards every task to an internal ThreadPool. More...
 

Namespaces

 

Detailed Description

Multi-threaded variant of MessageLoop that forwards tasks to an internal ThreadPool.

MultiLoop reuses every MessageLoop posting API but offloads execution to a pool of worker threads. The dispatcher thread still dequeues tasks; instead of running each task inline it hands them to the worker pool through the on_task_changed hook. Callers post via post_task / exec_task exactly as with a single-threaded loop.

Dispatch behaviour (delegated to the underlying ThreadPool)

Forwarded tasks are submitted to the pool's single shared queue. Whichever worker is idle dequeues the next task, woken by the pool's condition variable. There is no round-robin, least-loaded, or hash-based worker selection: cross-worker ordering and thread affinity are not guaranteed.

Worker diagram
*             post_task / exec_task
*                       |
*                       v
*      +---------------------------------+
*      |       MessageLoop queue         |
*      +----------------+----------------+
*                       |
*                       v
*      +---------------------------------+
*      |       dispatcher thread         |
*      +-------+--------+--------+-------+
*              |        |        |
*              v        v        v
*        +-------+  +-------+  +-------+
*        | Wkr 1 |  | Wkr 2 |  | Wkr N |
*        +-------+  +-------+  +-------+
* 
Differences vs MessageLoop
  • Tasks may run concurrently on multiple worker threads; execution order is unspecified.
  • is_in_same_thread returns true for the dispatcher and every worker thread.
  • on_begin / on_end are still invoked once on the dispatcher; they construct and tear down the internal pool.
  • When the pool rejects a forwarded task (for example a zero-worker pool) the base MessageLoop::on_task_changed runs the callback inline on the dispatcher.
Example
loop.async_run();
for (int i = 0; i < 100; ++i) {
loop.post_task([i] { process(i); });
}
loop.wait_for_idle();
loop.quit();
loop.wait_for_quit();
Note
Shared state touched inside callbacks must be protected externally. Timers attached to a MultiLoop fire as queue tasks; the dispatcher forwards them to a worker. The destructor is defaulted; always call quit and wait_for_quit before destruction.