Skip to content

Commit 7dcb646

Browse files
committed
Add implementation of exercise3-40 and 3-41
1 parent cdeb556 commit 7dcb646

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
习题完成情况:
2626
- 章节一: 43/46
2727
- 章节二: 88/97
28-
- 章节三: 40/82
28+
- 章节三: 42/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-41.org

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#+LATEX_CLASS: ramsay-org-article
2+
#+LATEX_CLASS_OPTIONS: [oneside,A4paper,12pt]
3+
#+AUTHOR: Ramsay Leung
4+
5+
#+DATE: 2025-07-21 Mon 20:17
6+
7+
Ben 的方案并不能解决问题,因为读本身就是原子的,并不需要加锁的;给读加锁引入额外的开销又没有解决问题。
8+
9+
需要加锁的是 =withdraw= 或者 =deposit=, 因为它们是先读,再计算,最后把计算后的值写入,这三个操作需要是原子的.

chapter3/exercise3-42.org

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#+LATEX_CLASS: ramsay-org-article
2+
#+LATEX_CLASS_OPTIONS: [oneside,A4paper,12pt]
3+
#+AUTHOR: Ramsay Leung
4+
5+
#+DATE: 2025-07-21 Mon 20:23
6+
两个方案从实现效果来说是等价的,都是并发安全的。
7+
8+
主要的差别是原始实现每次调用创建新序列化过程,Ben的实现会预先创建序列化过程。
9+
10+
相当于Ben的实现是使用了面向对象编程中的单例,只在初始化时创建一次,而原始实现每次调用都创建新序列化过程。
11+
12+
所以Ben的方案内存占用和性能都更优。

0 commit comments

Comments
 (0)