Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 10_Ideas_and_Inspiration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

信号量可以实现为用于计数器的 `Mutex<u32>` 以及用于等待操作的 `Condvar` 的组合。然而,有几种方式能更有效地实现它。更值得关注的是,在支持类 futex 操作([第八章“futex”](./8_Operating_System_Primitives.md#futex))的平台上,它可以作为单个 AtomicU32(或者甚至 AtomicU8)更高效地实现。

最大值为 1 的信号量又是被称为*二进制*信号量,它可以用作构建其他原语的基石。例如,它可以通过初始化计数器、使用锁定的 wait 操作以及释放的 signal 操作来做用于 mutex。通过在 0 处初始化它,它可以被用作信号,就像条件变量一样。例如,在标准库 `std::thread` 的 `park()` 的 `unpark()` 函数可以实现与线程关联的二进制信号量上的 wait 和 signal 操作。
最大值为 1 的信号量又是被称为*二进制*信号量,它可以用作构建其他原语的基石。例如,它可以通过初始化计数器、使用锁定的 wait 操作以解锁的 signal 操作来做用于 mutex。通过在 0 处初始化它,它可以被用作信号,就像条件变量一样。例如,在标准库 `std::thread` 的 `park()` 的 `unpark()` 函数可以实现与线程关联的二进制信号量上的 wait 和 signal 操作。

> 注意,mutex 可以使用信号量来实现,而信号量可以使用 mutex(或者条件变量)来实现。建议避免使用基于 mutex 的信号量来实现基于信号量的 mutex,反之亦然。

Expand Down Expand Up @@ -56,7 +56,7 @@

从链表列表中分离元素后,你将遇到与之前相同的问题:它会等待,直到你解除分配(或者以其他方式宣称所有权)。在这种情况下,我们讨论的基本的 RCU 模式的相同解决方案也有效。

总的来说,你可以基于原子指针上的比较并交换操作,构建各种精心设计的无锁数据结构,但是你将总是需要一个号的策略来释放分配或者以其他方式收回分配的所有权
总的来说,你可以基于原子指针上的比较并交换操作,构建各种精心设计的无锁数据结构,但是你将总是需要一个好的策略来释放分配或者以其他方式收回分配的所有权

进一步阅读:

Expand Down
50 changes: 25 additions & 25 deletions 1_Basic_of_Rust_Concurrency.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions 2_Atomics.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ fn main() {

在我们的示例中,这两个都不是大问题。最糟糕的情况是向用户提交了不准确的平均值。

如果我们想要避免这个,我们可以把这三个统计数据放到一个 Mutex 中。然后,在更新三个数字时,我们短暂地锁定 mutex,这三个数字不再是原子的。这有效地转变三次更新为单个原子操作,代价是锁定和释放 mutex,并且可能临时地阻塞线程。
如果我们想要避免这个,我们可以把这三个统计数据放到一个 Mutex 中。然后,在更新三个数字时,我们短暂地锁定 mutex,这三个数字不再是原子的。这有效地转变三次更新为单个原子操作,代价是锁定和解锁 mutex,并且可能临时地阻塞线程。

### 示例:ID 分配

Expand Down Expand Up @@ -497,7 +497,7 @@ fn allocate_new_id() -> u32 {
有关详细信息,请查看该方法的文档。

我们不会在本书中使用 <code>fetch_update</code> 方法,因此我们可以专注于单个原子操作。
</div>
</div>

### 示例:惰性一次性初始化

Expand Down
664 changes: 661 additions & 3 deletions 3_Memory_Ordering.md

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions 9_Building_Our_Own_Locks.md

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Rust Atomics and Locks

## [第一章:Rust 并发基础](./1_Basic_of_Rust_Concurrency.md)

* [Rust 中的线程](./1_Basic_of_Rust_Concurrency.md#rust-中的线程)
Expand Down Expand Up @@ -44,9 +42,9 @@
* [产生和加入](./3_Memory_Ordering.md#产生和加入)
* [Relaxed 排序](./3_Memory_Ordering.md#relaxed-排序)
* [Release 和 Acquire 排序](./3_Memory_Ordering.md#release-和-acquire-排序)
* [示例:「锁」](./3_Memory_Ordering.md#示例锁)
* [示例:锁定](./3_Memory_Ordering.md#示例锁定)
* [示例:使用间接的方式惰性初始化](./3_Memory_Ordering.md#示例使用间接的方式惰性初始化)
* [消费排序](./3_Memory_Ordering.md#消费排序)
* [Consume 排序](./3_Memory_Ordering.md#consume-排序)
* [顺序一致性排序](./3_Memory_Ordering.md#顺序一致性排序)
* [屏障(Fence)](./3_Memory_Ordering.md#屏障fence2)
* [常见的误解](./3_Memory_Ordering.md#常见的误解)
Expand Down
5 changes: 5 additions & 0 deletions assets/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
background: #462e54;
}

.markdown-body code {
background: rgb(31, 91, 62);
color: white;
}

}
Binary file added picture/raal_0301.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture/raal_0302.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture/raal_0303.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture/raal_0304.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture/raal_0305.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.