weak 和 strong 的区别

本文通过生动的例子解释了强指针和弱指针的区别:强指针如同栓狗的绳子,只有当所有强指针消失,对象才会被释放;而弱指针则像是孩子手指的方向,无论多少个,都无法阻止对象的释放。
觉得讲的很容易理解  

The difference is that an object will be deallocated as soon as there are no strong pointers to it. Even if weak pointers point to it, once the last strong pointer is gone, the object will be deallocated, and all remaining weak pointers will be zeroed out.

Perhaps an example is in order.

Imagine our object is a dog, and that the dog wants to run away (be deallocated).

Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.

Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.

As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.

(weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放  ,即使还有weak型指针指向它。

一旦最后一个strong型指针离去 ,这个对象将被释放,所有剩余的weak型指针都将被清除。

可能有个例子形容是妥当的。

想象我们的对象是一条狗,狗想要跑掉(被释放)。

strong型指针就像是栓住的狗。只要你用牵绳挂住狗,狗就不会跑掉。如果有5个人牵着一条狗(5个strong型指针指向1个对象),除非5个牵绳都脱落 ,否着狗是不会跑掉的。

weak型指针就像是一个小孩指着狗喊到:“看!一只狗在那” 只要狗一直被栓着,小孩就能看到狗,(weak指针)会一直指向它。只要狗的牵绳脱落,狗就会跑掉,不管有多少小孩在看着它。

只要最后一个strong型指针不再指向对象,那么对象就会被释放,同时所有的weak型指针都将会被清除。

### C++中 `compare_exchange_weak` `compare_exchange_strong` 的区别及用法 在C++的并发编程中,`std::atomic` 提供了两种重要的原子操作函数:`compare_exchange_weak` `compare_exchange_strong`。这两种函数都用于实现比较交换操作,但它们的行为适用场景有所不同。 #### 1. `compare_exchange_weak` 的行为与特点 `compare_exchange_weak` 是一种“弱”版本的比较交换操作。它尝试将某个值与预期值进行比较,如果两者相等,则将目标值替换为期望的新值;否则,将目标值存储到预期值变量中。然而,`compare_exchange_weak` 可能会由于硬件或编译器优化的原因而失败,即使条件满足也可能返回 `false`。这种失败被称为“假失败”,通常需要通过循环重试来处理[^2]。 ```cpp bool compare_exchange_weak(T& expected, T desired, std::memory_order success, std::memory_order failure); ``` - **参数**: - `expected`:当前预期值,若比较失败,会被更新为目标值。 - `desired`:希望设置的新值。 - `success`:成功时使用的内存顺序。 - `failure`:失败时使用的内存顺序。 - **特点**: - 允许假失败(spurious failure),可能需要多次尝试才能成功。 - 性能通常优于 `compare_exchange_strong`,因为它对硬件资源的需求较低。 #### 示例代码 ```cpp #include <iostream> #include <atomic> std::atomic<int> value(0); void example_compare_exchange_weak() { int expected = 0; int desired = 42; while (!value.compare_exchange_weak(expected, desired, std::memory_order_acq_rel, std::memory_order_relaxed)) { expected = 0; // 重置预期值 } std::cout << "Value after exchange: " << value.load() << std::endl; } ``` #### 2. `compare_exchange_strong` 的行为与特点 `compare_exchange_strong` 是一种“强”版本的比较交换操作。与 `compare_exchange_weak` 不同,它不会出现假失败的情况。只要条件满足,操作一定会成功。因此,使用 `compare_exchange_strong` 时通常不需要额外的循环重试逻辑[^3]。 ```cpp bool compare_exchange_strong(T& expected, T desired, std::memory_order success, std::memory_order failure); ``` - **参数**: - 同 `compare_exchange_weak`。 - **特点**: - 不允许假失败,操作要么成功,要么失败且提供明确原因。 - 性能可能略逊于 `compare_exchange_weak`,因为需要更多的同步开销。 #### 示例代码 ```cpp #include <iostream> #include <atomic> std::atomic<int> value(0); void example_compare_exchange_strong() { int expected = 0; int desired = 42; if (value.compare_exchange_strong(expected, desired, std::memory_order_acq_rel, std::memory_order_relaxed)) { std::cout << "Exchange succeeded" << std::endl; } else { std::cout << "Exchange failed, current value: " << expected << std::endl; } } ``` #### 3. 主要区别 | 特性 | `compare_exchange_weak` | `compare_exchange_strong` | |-------------------------|--------------------------------------------|------------------------------------| | 假失败 | 可能发生 | 不会发生 | | 循环重试需求 | 通常需要 | 不需要 | | 性能 | 较高 | 较低 | | 硬件资源消耗 | 较低 | 较高 | #### 4. 使用场景 - **`compare_exchange_weak`**:适用于性能敏感的场景,尤其是在假失败对程序逻辑无影响的情况下。由于其潜在的假失败特性,通常需要结合循环使用[^4]。 - **`compare_exchange_strong`**:适用于逻辑复杂、不允许假失败的场景。尽管性能稍逊,但在某些情况下可以简化代码逻辑。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值