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 89ee36d

Browse files
committed
修改部分教案措辞,更新视频代码
1 parent 9fa4d5b commit 89ee36d

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

‎code/04同步操作/async_progress_bar/async_progress_bar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ class async_progress_bar : public QMainWindow{
6767
QPushButton* button{};
6868
QPushButton* button2{};
6969
Ui::async_progress_barClass ui{};
70-
std::future<void>future;
70+
std::future<void>future;
7171
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <chrono>
2+
#include <iostream>
3+
#include <iomanip>
4+
#include <future>
5+
using namespace std::chrono_literals;
6+
7+
int main(){
8+
using namespace std::chrono;
9+
auto future = std::async(std::launch::deferred, []{
10+
std::cout << "deferred\n";
11+
});
12+
13+
if (future.wait_for(35ms) == std::future_status::deferred)
14+
std::cout << "future_status::deferred " << "正在延迟执行\n";
15+
16+
future.wait(); // 在 wait() 或 get() 调用时执行,不创建线程
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <chrono>
3+
#include <thread>
4+
#include <mutex>
5+
#include <condition_variable>
6+
#include <Windows.h>
7+
#pragma comment(lib,"winmm.lib")
8+
9+
using namespace std::chrono_literals;
10+
11+
std::condition_variable cv;
12+
bool done{};
13+
std::mutex m;
14+
15+
bool wait_loop() {
16+
const auto timeout = std::chrono::steady_clock::now() + 500ms;
17+
std::unique_lock<std::mutex> lk{ m };
18+
if (!cv.wait_until(lk, timeout, [] {return done; })) {
19+
std::cout << "超时 500ms\n";
20+
return false;
21+
}
22+
return true;
23+
}
24+
25+
int main() {
26+
std::thread t{ wait_loop };
27+
std::this_thread::sleep_for(400ms);
28+
done = true;
29+
cv.notify_one();
30+
t.join();
31+
}

‎code/ModernCpp-ConcurrentProgramming-Tutorial/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "C
1212
add_compile_options("-finput-charset=UTF-8" "-fexec-charset=UTF-8")
1313
endif()
1414

15-
add_executable(${PROJECT_NAME} "32限时等待-时钟.cpp")
15+
add_executable(${PROJECT_NAME} "34限时等待-时间点.cpp")
1616

1717

1818
# 设置 SFML 的 CMake 路径

‎md/03共享数据.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ void process_data(){
647647
648648
保护共享数据并非必须使用互斥量,互斥量只是其中一种常见的方式而已,对于一些特殊的场景,也有专门的保护方式,比如**对于共享数据的初始化过程的保护**。我们通常就不会用互斥量,**这会造成很多的额外开销**
649649
650-
我们不想为各位介绍其它乱七八糟的各种保护初始化的方式,我们只介绍三种:**双检锁(错误)****使用 `std::call_once`****静态局部变量初始化在 C++11 是线程安全**
650+
我们不想为各位介绍其它乱七八糟的各种保护初始化的方式,我们只介绍三种:**双检锁(错误)****使用 `std::call_once`****静态局部变量初始化从 C++11 开始是线程安全**
651651
652652
1. **双检锁(错误)线程不安全**
653653

‎md/04同步操作.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ template<
11821182
> class time_point;
11831183
```
11841184
1185-
如你所见,它的第二个模板参数的时间单位,默认是根据第一个参数时钟得到的,所以假设有类型:
1185+
如你所见,它的第二个模板参数是**时间段**,就是时间的间隔,其实也就可以理解为表示时间点的**精度**,默认是根据第一个参数时钟得到的,所以假设有类型:
11861186
11871187
```cpp
11881188
std::chrono::time_point<std::chrono::system_clock>
@@ -1200,8 +1200,30 @@ std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::du
12001200
std::chrono::duration<long long,std::ratio<1, 10000000>> // // 100 nanoseconds
12011201
```
12021202
1203+
也就是说 `std::chrono::time_point<std::chrono::system_clock>` 的精度是 100 纳秒。
1204+
12031205
更多的问题参见[源码](https://github.com/microsoft/STL/blob/f54203f/stl/inc/__msvc_chrono.hpp#L644-L647)都很直观。
12041206
1207+
> 注意,这里的精度并非是实际的时间精度。时间和硬件系统等关系极大,以 windows 为例:
1208+
>
1209+
> Windows 内核中的时间间隔计时器默认每隔 **15.6** 毫秒触发一次中断。因此,如果你使用基于系统时钟的计时方法,默认情况下精度约为 15.6 毫秒。不可能达到纳秒级别。
1210+
>
1211+
> 由于这个系统时钟的限制,那些基于系统时钟的 API(例如 `Sleep()``WaitForSingleObject()` 等)的最小睡眠时间默认就是 15.6 毫秒左右。
1212+
>
1213+
> 如:
1214+
>
1215+
> ```cpp
1216+
> std::this_thread::sleep_for(std::chrono::milliseconds(1));
1217+
> ```
1218+
>
1219+
> 不过我们也可以使用系统 API 调整系统时钟的精度,需要链接 windows 多媒体库 **`winmm.lib`** ,然后使用 API:
1220+
>
1221+
> ```cpp
1222+
> timeBeginPeriod(1); // 设置时钟精度为 1 毫秒
1223+
> // todo..
1224+
> timeEndPeriod(1); // 恢复默认精度
1225+
> ```
1226+
12051227
---
12061228
12071229
同样的,时间点也支持加减以及比较操作。

0 commit comments

Comments
(0)

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