VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
multi_loop.h
浏览该文件的文档.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file multi_loop.h
26 * @brief Multi-threaded event loop backed by a pool of worker threads sharing one task queue.
27 *
28 * @details
29 * @c MultiLoop extends @c MessageLoop by running @p thread_num worker threads that all
30 * dequeue and execute tasks from the same queue. This enables parallel task execution
31 * without changing the posting API: callers still call @c post_task() and @c exec_task()
32 * exactly as with a single-threaded @c MessageLoop.
33 *
34 * Differences from @c MessageLoop:
35 * - Tasks may run concurrently on multiple threads; they are @b not serialised.
36 * - @c is_in_same_thread() returns @c true if the caller is any of the worker threads.
37 * - @c on_begin() and @c on_end() are called once per worker thread.
38 * - @c on_task_changed() is called from the executing worker thread.
39 *
40 * @note
41 * - Shared state accessed inside task callbacks must be protected by its own synchronisation.
42 * - Timers attached to a @c MultiLoop still fire on one of the worker threads (non-deterministic).
43 * - The destructor waits for all worker threads to finish.
44 *
45 * @par Example
46 * @code
47 * vlink::MultiLoop loop(4); // 4 worker threads
48 * loop.async_run();
49 *
50 * for (int i = 0; i < 100; ++i) {
51 * loop.post_task([i]() { process(i); });
52 * }
53 *
54 * loop.wait_for_idle();
55 * loop.quit();
56 * @endcode
57 */
58
59#pragma once
60
61#include <memory>
62
63#include "./message_loop.h"
64
65namespace vlink {
66
67/**
68 * @class MultiLoop
69 * @brief Multi-threaded variant of @c MessageLoop running tasks on a worker-thread pool.
70 *
71 * @details
72 * All threads share the same task queue. Tasks are @b not guaranteed to execute in order.
73 */
75 public:
76 /**
77 * @brief Constructs a @c MultiLoop with the default @c kNormalType queue and @p thread_num workers.
78 *
79 * @param thread_num Number of worker threads. Default: 4.
80 */
81 explicit MultiLoop(size_t thread_num = 4U);
82
83 /**
84 * @brief Constructs a @c MultiLoop with a specific queue type.
85 *
86 * @param thread_num Number of worker threads.
87 * @param type Queue implementation type (see @c MessageLoop::Type).
88 */
89 explicit MultiLoop(size_t thread_num, Type type);
90
91 /**
92 * @brief Destructor. Quits the loop and joins all worker threads.
93 */
94 ~MultiLoop() override;
95
96 /**
97 * @brief Returns @c true if the calling thread is one of the worker threads.
98 *
99 * @return @c true if called from any thread owned by this @c MultiLoop.
100 */
101 [[nodiscard]] bool is_in_same_thread() const override;
102
103 protected:
104 /**
105 * @brief Called once on each worker thread immediately after it starts.
106 *
107 * @details
108 * Override to perform per-thread initialisation (e.g., setting thread name or affinity).
109 */
110 void on_begin() override;
111
112 /**
113 * @brief Called once on each worker thread just before it exits.
114 *
115 * @details
116 * Override to perform per-thread cleanup.
117 */
118 void on_end() override;
119
120 /**
121 * @brief Called on the worker thread executing the task, before the task runs.
122 *
123 * @param callback The task about to be executed.
124 * @param start_time Millisecond timestamp at which the task was dequeued.
125 */
126 void on_task_changed(Callback&& callback, uint32_t start_time) override;
127
128 private:
129 std::unique_ptr<struct MultiLoopImpl> impl_;
130
132};
133
134} // namespace vlink
#define VLINK_EXPORT
定义 macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184
Single-threaded event loop with three queue types, timer management and task scheduling.