VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
multi_loop.h File Reference

Multi-threaded event loop backed by a pool of worker threads sharing one task queue. More...

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

Go to the source code of this file.

Classes

 Multi-threaded variant of MessageLoop running tasks on a worker-thread pool. More...

Namespaces

Detailed Description

Multi-threaded event loop backed by a pool of worker threads sharing one task queue.

MultiLoop extends MessageLoop by running thread_num worker threads that all dequeue and execute tasks from the same queue. This enables parallel task execution without changing the posting API: callers still call post_task() and exec_task() exactly as with a single-threaded MessageLoop.

Differences from MessageLoop:

  • Tasks may run concurrently on multiple threads; they are not serialised.
  • is_in_same_thread() returns true if the caller is any of the worker threads.
  • on_begin() and on_end() are called once per worker thread.
  • on_task_changed() is called from the executing worker thread.
Note
  • Shared state accessed inside task callbacks must be protected by its own synchronisation.
  • Timers attached to a MultiLoop still fire on one of the worker threads (non-deterministic).
  • The destructor waits for all worker threads to finish.
Example
vlink::MultiLoop loop(4); // 4 worker threads
loop.async_run();
for (int i = 0; i < 100; ++i) {
loop.post_task([i]() { process(i); });
}
loop.wait_for_idle();
loop.quit();