Skip to content

Commit 6af44d9

Browse files
committed
feat: add C++ and DistributedSystem categories, add README.md
1 parent 0a2734f commit 6af44d9

File tree

9 files changed

+449
-0
lines changed

9 files changed

+449
-0
lines changed

C++/.clang-format

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: Google
4+
PointerAlignment: Right
5+
DerivePointerAlignment: false
6+
AccessModifierOffset: -4
7+
AllowShortFunctionsOnASingleLine: Inline
8+
AllowShortIfStatementsOnASingleLine: false
9+
AllowShortLoopsOnASingleLine: false
10+
BraceWrapping:
11+
AfterClass: true
12+
AfterEnum: true
13+
AfterFunction: true
14+
AfterNamespace: true
15+
AfterStruct: true
16+
AfterUnion: true
17+
BeforeCatch: true
18+
BeforeElse: true
19+
AfterCaseLabel: true
20+
AfterExternBlock: true
21+
AfterObjCDeclaration: true
22+
AfterControlStatement: true
23+
BreakBeforeBraces: Attach
24+
ColumnLimit: 120
25+
IndentWidth: 4
26+
TabWidth: 4
27+
IncludeBlocks: Preserve
28+
29+
SpacesBeforeTrailingComments: 2 # 单行代码注释前填充两个空格
30+
AlignConsecutiveMacros: true
31+
AlignConsecutiveAssignments: false
32+
Cpp11BracedListStyle: true
33+
SpaceInEmptyBlock: true # 空代码块使用单个空格填充

C++/20/chrono/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# chrono
2+
3+
## 时钟
4+
5+
        按照官方的说法,`时钟` 是由一个时间起点及固定嘀嗒速率(Tick rate)组成的。比如,我们可以认为 `C` 通过 `time` 得到的时间,就是由一个以 1970 年 1 月 1 日零点为时间起点,嘀嗒速率固定为 1 秒的时钟返回的值。`C++` 提供了几种常用的时钟类型。
6+
7+
## 时间点
8+
9+
## 时长
10+
11+
## 当天时刻
12+
13+
## 日历
14+
15+
## 时区

C++/20/chrono/code/main.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: modnarshen
3+
* @Date: 2022/09/27 16:55:20
4+
* @LastEditors: modnarshen
5+
* @Description: std::chrono
6+
*/
7+
#include <chrono>
8+
#include <iostream>
9+
10+
int main(int argc, char *argv[]) {
11+
// 时间点
12+
auto now = std::chrono::system_clock::now();
13+
// 时长
14+
std::chrono::duration<int64_t, std::nano> ss = now.time_since_epoch();
15+
// 时长
16+
auto hh = std::chrono::floor<std::chrono::days>(ss);
17+
// 时间点
18+
std::chrono::time_point<std::chrono::system_clock, std::chrono::days> tp =
19+
std::chrono::floor<std::chrono::days>(now);
20+
// hh_mm_ss 使用的是时长
21+
std::chrono::hh_mm_ss<std::chrono::microseconds> tod{
22+
std::chrono::duration_cast<std::chrono::microseconds>(ss - hh)};
23+
// year_month_day 使用的是时间点
24+
const std::chrono::year_month_day ymd(tp);
25+
std::cout << static_cast<int>(ymd.year()) << static_cast<unsigned>(ymd.month()) << static_cast<unsigned>(ymd.day())
26+
<< " " << tod.hours().count() << ":" << tod.minutes().count() << ":" << tod.seconds().count() << "."
27+
<< tod.subseconds().count() << std::endl;
28+
return 0;
29+
}

C++/20/coroutine/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# 协程
2+
3+
- C++20
4+
5+
## Coroutine Handles
6+
7+
```cpp
8+
#include <concepts>
9+
#include <coroutine>
10+
#include <exception>
11+
#include <iostream>
12+
13+
struct ReturnObject {
14+
struct promise_type {
15+
ReturnObject get_return_object() { return {}; }
16+
std::suspend_never initial_suspend() { return {}; }
17+
std::suspend_never final_suspend() noexcept { return {}; }
18+
void unhandled_exception() { }
19+
};
20+
};
21+
22+
struct Awaiter {
23+
std::coroutine_handle<> *hp_;
24+
constexpr bool await_ready() const noexcept { return false; }
25+
void await_suspend(std::coroutine_handle<> h) { *hp_ = h; }
26+
constexpr void await_resume() const noexcept { }
27+
};
28+
29+
ReturnObject counter(std::coroutine_handle<> *continuation_out) {
30+
Awaiter a{continuation_out};
31+
for (unsigned i = 0;; ++i) {
32+
co_await a;
33+
std::cout << "counter: " << i << std::endl;
34+
}
35+
}
36+
37+
void main1() {
38+
std::coroutine_handle<> h;
39+
counter(&h);
40+
for (int i = 0; i < 3; ++i) {
41+
std::cout << "In main1 function\n";
42+
h();
43+
}
44+
h.destroy();
45+
}
46+
```
47+
48+
&#160; &#160; &#160; &#160; C++20 使用 `co_await` 将当前协程挂起时,当前的状态、变量等数据都会绑定在堆中,并创建了一个可调用对象用于后续唤醒当前协程以恢复执行。这个可调用对象的类型,就是 `std::coroutine_handle<>`。
49+
50+
&#160; &#160; &#160; &#160; 当执行 `co_await a;` 时,需要 `a` 的类型必须支持一些指定的方法,而更常见的场景是,需要将 `a` 视为一个 **awaitable** 的对象或一个 **awaiter**。
51+
52+
&#160; &#160; &#160; &#160; `await_ready` 接口实际属于一个优化接口,每次执行 `co_await` 时,先执行此接口来判断是否要将当前协程挂起。当然,也可以在 `await_suspend` 中补充逻辑达到相同的优化目的,但需要注意的是,在调用 `await_suspend` 之前,编译器就已经需要将协程状态等数据绑定到可通过 coroutine_handle 访问的堆对象了,而这一操作本身就属于是「重量级」操作了。
53+
54+
&#160; &#160; &#160; &#160; 由此可知,`std::await_always::await_ready` 总是会返回 `false`,而 `std::await_never::await_ready` 则总是会返回 `true`,而这两个类型的其他接口内都没有任何代码逻辑。
55+
56+
## The coroutine return object
57+
58+
```cpp
59+
60+
struct ReturnObject2 {
61+
struct promise_type {
62+
ReturnObject2 get_return_object() {
63+
// Uses C++20 designated initializer syntax
64+
return {.h_ = std::coroutine_handle<promise_type>::from_promise(*this)};
65+
}
66+
std::suspend_never initial_suspend() { return {}; }
67+
std::suspend_never final_suspend() noexcept { return {}; }
68+
void unhandled_exception() { }
69+
};
70+
71+
std::coroutine_handle<promise_type> h_;
72+
operator std::coroutine_handle<promise_type>() const { return h_; }
73+
// A coroutine_handle<promise_type> converts to coroutine_handle<>
74+
operator std::coroutine_handle<>() const { return h_; }
75+
};
76+
77+
ReturnObject2 counter2() {
78+
for (unsigned i = 0;; ++i) {
79+
co_await std::suspend_always{};
80+
std::cout << "counter2: " << i << std::endl;
81+
}
82+
}
83+
84+
void main2() {
85+
std::coroutine_handle<> h = counter2();
86+
for (int i = 0; i < 3; ++i) {
87+
std::cout << "In main2 function\n";
88+
h();
89+
}
90+
h.destroy();
91+
}
92+
```
93+
94+
&#160; &#160; &#160; &#160;`promise_type` 定义中,可以使用静态方法 `coroutine_handle::from_promise` 来获取协程处理程序(coroutine handle)。
95+
96+
&#160; &#160; &#160; &#160; 与示例 1 的代码不同,示例 2 中不再需要使用 awaiter (await_suspend)来保存 coroutine_handle 了,所以这里可以直接使用 `co_wait std::suspend_always{}` 来实现相同的挂起协程的效果。
97+
98+
&#160; &#160; &#160; &#160; 此外,尽管在 main2 函数的第一行,通过赋值操作会使得 counter2 返回的协程对象离开函数体被“销毁”,但我们可以将 coroutine_handle 理解为指针,也就是说尽管旧的指针对象已经被销毁了,但我们依然用 h 缓存了指针对象的值,我们依旧可以通过 h 来访问其指向的协程对象,因为这个协程对象本身是不会被销毁掉的。这也就是说,调用 counter2 的同时,需要显示调用 `h.destroy()` 来释放掉这段协程对象空间,否则会存在内存泄漏问题。值得一提的是,如果这里没有使用 h 来“接收” counter2 的返回值,那么就已经存在内存泄漏的问题了。

0 commit comments

Comments
 (0)