Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 302a53b

Browse files
committed
1. 修改第四章时间那一块的内容,增加描述。
2. 完善线程池的使用的内容。 Mq-b#12
1 parent 9f61276 commit 302a53b

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

‎md/04同步操作.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,12 @@ using years = duration<int, ratio_multiply<ratio<146097, 400>, days::period>>;
928928
using months = duration<int, ratio_divide<years::period, ratio<12>>>;
929929
```
930930
931+
如果没有指明 `duration` 的第二个非类型模板参数,那么代表默认 `std::ratio<1>`,比如 `seconds` 也就是一秒。
932+
931933
如上,是 MSVC STL 定义的,看似有一些没有使用 `ratio` 作为第二个参数,其实也还是别名罢了,[](https://github.com/microsoft/STL/blob/daeb0a6/stl/inc/ratio#L262-L277):
932934
933935
```cpp
934-
using milli = ratio<1, 1000>;
936+
using milli = ratio<1, 1000>;// 千分之一秒,也就是一毫秒了
935937
```
936938
937939
并且为了方便使用,在 C++14 标准库增加了时间字面量,存在于 `std::chrono_literals` 命名空间中,让我们得以简单的使用:
@@ -955,7 +957,30 @@ std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(ms);
955957
std::cout << s.count() << '\n';
956958
```
957959
958-
这里的结果是截断的,而不会进行所谓的四舍五入,最终的值是 `3`
960+
这里的结果是**截断**的,而不会进行所谓的四舍五入,`3999` 毫秒,也就是 `3.999` 秒最终的值是 `3`
961+
962+
> 很多时候这并不是我们想要的,比如我们想要的其实是输出 `3.999` 秒,而不是 `3` 秒 或者 `3999` 毫秒。
963+
>
964+
> seconds 是 `duration<long long>` 这意味着它无法接受浮点数,我们直接改成 `duration<double>` 即可:
965+
>
966+
> ```cpp
967+
> std::chrono::duration<double> s = std::chrono::duration_cast<std::chrono::duration<double>>(ms);
968+
> ```
969+
>
970+
> 当然了,这样写很冗余,并且这种形式的转换是可以直接隐式的,也就是其实我们可以直接:
971+
>
972+
> ```cpp
973+
> std::chrono::duration<double> s = ms;
974+
> ```
975+
>
976+
> 无需使用 `duration_cast`,可以直接隐式转换。
977+
>
978+
> 另外我们用的 `duration` 都是省略了 `ratio` 的,其实默认类型就是 `ratio<1>`,代表一秒。参见源码[声明](https://github.com/microsoft/STL/blob/0be5257/stl/inc/__msvc_chrono.hpp#L80-L81):
979+
>
980+
> ```cpp
981+
> _EXPORT_STD template <class _Rep, class _Period = ratio<1>>
982+
> class duration;
983+
> ```
959984
960985
时间库支持四则运算,可以对两个时间段进行加减乘除。时间段对象可以通过 [`count()`](https://zh.cppreference.com/w/cpp/chrono/duration/count) 成员函数获得计次数。例如 `std::chrono::milliseconds{123}.count()` 的结果就是 123。
961986
@@ -1207,7 +1232,7 @@ async_progress_bar::async_progress_bar(QWidget *parent)
12071232
12081233
### 跨平台兼容性
12091234
1210-
C++11 的 `std::this_thread::get_id()` 返回的内部类型没办法直接转换为 `unsigned int`,我们就直接使用了 win32 的 API *`_Thrd_id()`* 了。如果您是 Linux 之类的环境,使用 [POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/) 接口 [*`pthread_self()`*](https://pubs.opengroup.org/onlinepubs/009696699/functions/pthread_self.html)。
1235+
C++11 的 `std::this_thread::get_id()` 返回的内部类 [`std::thread::id`](https://zh.cppreference.com/w/cpp/thread/thread/id) 没办法直接转换为 `unsigned int`,我们就直接使用了 win32 的 API *`_Thrd_id()`* 了。如果您是 Linux 之类的环境,使用 [POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/) 接口 [*`pthread_self()`*](https://pubs.opengroup.org/onlinepubs/009696699/functions/pthread_self.html)。
12111236
12121237
### 实践建议
12131238

‎md/详细分析/04线程池.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ thread_pool::thread_pool()
141141

142142
---
143143

144-
Boost.Asio 的线程池对象在析构时会自动调用相关的清理方法,但你也可以手动进行控制:
144+
Boost.Asio 的线程池对象在[析构](https://github.com/boostorg/asio/blob/44238d033e1503c694782925d647811380a067c2/include/boost/asio/impl/thread_pool.ipp#L98-L103)时会自动调用相关的清理方法,但你也可以手动进行控制
145145

146146
```cpp
147147
thread_pool::~thread_pool()
@@ -158,6 +158,16 @@ thread_pool::~thread_pool()
158158

159159
> 此处可阅读部分源码,帮助理解与记忆
160160
161+
`Boost.Asio` 提供的线程池使用十分简单,接口高度封装,几乎无需关心底层具体实现,易于使用。
162+
163+
我们的操作几乎只需创建线程池对象、将任务加入线程池、在需要时调用 `join()`
164+
165+
```cpp
166+
boost::asio::thread_pool pool{4}; // 创建线程池
167+
boost::asio::post(pool, task); // 将任务加入线程池
168+
pool.join(); // 等待任务完成 (或者析构自动调用)
169+
```
170+
161171
## 实现线程池
162172
163173
实现一个普通的能够满足日常开发需求的线程池实际上非常简单,也只需要一百多行代码。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /