Skip to content

Commit 7877d0e

Browse files
committed
补充第五章 std::atomic_flag 的描述,自旋锁与互斥锁优劣对比问题
1 parent acda8c6 commit 7877d0e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

md/05内存模型与原子操作.md

+6
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ bool r = f.test_and_set();
275275

276276
有限的特性使得 `std::atomic_flag` 非常适合用作制作**自旋锁**
277277

278+
> 自旋锁可以理解为一种***忙等锁***,因为它在等待锁的过程中不会主动放弃 CPU,而是持续检查锁的状态。
279+
>
280+
> 与此相对,`std::mutex` 互斥量是一种***睡眠锁***。当线程请求锁(`lock()`)而未能获取时,它会放弃 CPU 时间片,让其他线程得以执行,从而有效利用系统资源。
281+
>
282+
> 从性能上看,自旋锁的响应更快,但是睡眠锁更加节省资源,高效。
283+
278284
```cpp
279285
class spinlock_mutex {
280286
std::atomic_flag flag{};

0 commit comments

Comments
 (0)