Skip to content

Commit 5290a16

Browse files
committed
docs: 增加doxygen.conf,便于生成本地文档
1 parent c8df93b commit 5290a16

File tree

4 files changed

+87
-19
lines changed

4 files changed

+87
-19
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- 稳定的:利用`std::function`的小任务优化减少内存碎片、拥有良好的异步线程异常处理机制。
2525
- 兼容性:纯C++11实现,跨平台,且兼容C++11以上版本。
2626

27+
2728
## 主要模块
2829

2930
### **workbranch**
@@ -311,6 +312,15 @@ threads: 12 | tasks: 100000000 | time-cost: 5.17316 (s)
311312

312313
## 如何使用
313314

315+
#### 生成doxygen文档
316+
请提前安装doxygen
317+
``` shell
318+
# 与workspace同级目录中(Linux)
319+
doxygen ./doxygen.conf
320+
```
321+
生成的文档在`workspace/docs/`中,可以在浏览器中打开`workspace/docs/html/index.html`并查看接口。
322+
323+
314324
#### 简单使用
315325

316326
```shell

doxygen.conf

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 项目名称
2+
PROJECT_NAME = workspace
3+
4+
# 项目版本号
5+
PROJECT_NUMBER = 1.0
6+
7+
# 输入目录
8+
INPUT = include README.md
9+
10+
# 输出目录
11+
OUTPUT_DIRECTORY = docs
12+
13+
# 递归处理子目录
14+
RECURSIVE = YES
15+
16+
# 匹配的文件模式
17+
FILE_PATTERNS = *.cpp *.h
18+
19+
# 使用Markdown格式处理注释
20+
MARKDOWN_SUPPORT = YES
21+
22+
# 生成的文档类型
23+
GENERATE_HTML = YES
24+
GENERATE_XML = YES
25+
26+
# 排除某些文件
27+
EXCLUDE_PATTERNS = */tests/*
28+
29+
# 自动生成函数的调用图
30+
CALL_GRAPH = YES
31+
32+
# 自动生成函数的调用关系图
33+
CALLER_GRAPH = YES
34+
35+
# 自动生成类的继承关系图
36+
COLLABORATION_GRAPH = YES
37+
38+
# 自动生成文件的依赖关系图
39+
INCLUDE_GRAPH = YES
40+
41+
# 自动生成文件的包含关系图
42+
INCLUDED_BY_GRAPH = YES
43+
44+
# 是否生成源代码文件的副本
45+
SOURCE_BROWSER = YES
46+
47+
# 是否生成内部类的文档
48+
INLINE_INHERITED_MEMB = YES
49+
50+
# 是否生成私有类成员的文档
51+
EXTRACT_PRIVATE = YES
52+
53+
# 是否生成静态成员的文档
54+
EXTRACT_STATIC = YES
55+
56+
# 强制提取所有文档内的所有元素
57+
EXTRACT_ALL = YES

include/workspace/workbranch.h

+13-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class workbranch {
1717
using worker = autothread<detach>;
1818
using worker_map = std::map<worker::id, worker>;
1919

20-
sz_t decline = 0;
20+
sz_t decline = 0;
2121
sz_t task_done_workers = 0;
2222
bool is_waiting = false;
2323
bool destructing = false;
@@ -74,17 +74,17 @@ class workbranch {
7474

7575
/**
7676
* @brief Wait for all tasks done.
77-
* @brief This interface will pause all threads(workers) in workbranch to relieve system's stress.
78-
* @param timeout timeout for waiting
77+
* @brief This interface will pause all threads(workers) in workbranch to relieve system's stress.
78+
* @param timeout timeout for waiting (ms)
7979
* @return return true if all tasks done
8080
*/
8181
bool wait_tasks(unsigned timeout = -1) {
8282
bool res;
8383
{
8484
std::unique_lock<std::mutex> locker(lok);
85-
is_waiting = true;
85+
is_waiting = true; // task_done_workers == 0
8686
res = task_done_cv.wait_for(locker, std::chrono::milliseconds(timeout), [this]{
87-
return task_done_workers >= workers.size(); // use ">=" to avoid supervisor delete workers
87+
return task_done_workers >= workers.size(); // use ">=" to avoid supervisor delete workers
8888
});
8989
task_done_workers = 0;
9090
is_waiting = false;
@@ -117,7 +117,7 @@ class workbranch {
117117
* @return void
118118
*/
119119
template <typename T = normal, typename F,
120-
typename R = details::result_of_t<F>,
120+
typename R = details::result_of_t<F>,
121121
typename DR = typename std::enable_if<std::is_void<R>::value>::type>
122122
auto submit(F&& task) -> typename std::enable_if<std::is_same<T, normal>::value>::type {
123123
tq.push_back([task]{
@@ -128,7 +128,7 @@ class workbranch {
128128
} catch (...) {
129129
std::cerr<<"workspace: worker["<< std::this_thread::get_id()<<"] caught unknown exception\n"<<std::flush;
130130
}
131-
});
131+
}); // function
132132
}
133133

134134
/**
@@ -154,6 +154,7 @@ class workbranch {
154154
/**
155155
* @brief async execute tasks
156156
* @param task runnable object (sequence)
157+
* @param tasks other parameters
157158
* @return void
158159
*/
159160
template <typename T, typename F, typename... Fs>
@@ -172,7 +173,6 @@ class workbranch {
172173
/**
173174
* @brief async execute the task
174175
* @param task runnable object (normal)
175-
* @param dummy dummy
176176
* @return std::future<R>
177177
*/
178178
template <typename T = normal, typename F,
@@ -200,7 +200,6 @@ class workbranch {
200200
/**
201201
* @brief async execute the task
202202
* @param task runnable object (urgent)
203-
* @param dummy dummy
204203
* @return std::future<R>
205204
*/
206205
template <typename T, typename F,
@@ -226,16 +225,16 @@ class workbranch {
226225
}
227226

228227
private:
229-
// loop
228+
// thread's default loop
230229
void mission() {
231230
std::function<void()> task;
232231
while (true) {
233232
if (decline <= 0 && tq.try_pop(task)) {
234233
task();
235234
} else if (decline > 0) {
236235
std::lock_guard<std::mutex> lock(lok);
237-
if (decline > 0 && decline--) {
238-
workers.erase(std::this_thread::get_id());
236+
if (decline > 0 && decline--) { // double check
237+
workers.erase(std::this_thread::get_id());
239238
if (is_waiting)
240239
task_done_cv.notify_one();
241240
if (destructing)
@@ -247,9 +246,9 @@ class workbranch {
247246
std::unique_lock<std::mutex> locker(lok);
248247
task_done_workers++;
249248
task_done_cv.notify_one();
250-
thread_cv.wait(locker);
249+
thread_cv.wait(locker);
251250
} else {
252-
std::this_thread::yield();
251+
std::this_thread::yield();
253252
}
254253
}
255254
}

test/test_workbranch.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
int main() {
44

5-
wsp::workbranch br(2);
5+
wsp::workbranch br(2); // 2 threads
66

77
// add a worker
88
br.add_worker();
@@ -11,21 +11,23 @@ int main() {
1111
// delete a worker
1212
br.del_worker();
1313
br.del_worker();
14-
std::cout<<"workers: "<<br.num_workers()<<std::endl;
14+
std::cout<<"workers: "<<br.num_workers()<<std::endl; // 1 worker
1515

1616
// normal task
17-
br.submit([]{std::cout<<"<normal>"<<std::endl;});
17+
br.submit([]{std::cout<<"<normal>"<<std::endl;}); // FIFO
1818

1919
// normal task
2020
br.submit<wsp::task::nor>([]{std::cout<<"<normal>"<<std::endl;});
2121

2222
// urgent task, executed as soon as possible
23-
br.submit<wsp::task::urg>([]{std::cout<<"<urgent>"<<std::endl;}); //
23+
br.submit<wsp::task::urg>([]{std::cout<<"<urgent>"<<std::endl;});
2424

2525
// sequence task
2626
br.submit<wsp::task::seq>([]{std::cout<<"<sequence1>"<<std::endl;},
2727
[]{std::cout<<"<sequence2>"<<std::endl;},
28-
[]{std::cout<<"<sequence3>"<<std::endl;});
28+
[]{std::cout<<"<sequence3>"<<std::endl;});
2929
// wait for tasks done
3030
br.wait_tasks();
31+
32+
// distruct -> close the threadpool
3133
}

0 commit comments

Comments
 (0)