VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
multi_loop.h 文件参考

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

#include <memory>
#include "./message_loop.h"
multi_loop.h 的引用(Include)关系图:

浏览该文件的源代码.

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

命名空间

详细描述

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.
注解
  • 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();