auto_ptr 为什么被淘汰?

auto_ptr 为什么被淘汰?

std::auto_ptr 是 C++98 最早的智能指针,用于自动管理动态分配的内存。但在 C++11 引入 std::unique_ptrstd::shared_ptr 后,auto_ptr 被淘汰,并在 C++17 中被移除


1. auto_ptr 被淘汰的主要原因

❌ 1. auto_ptr 使用“所有权转移”导致意外错误

auto_ptr 采用 “所有权转移” (Ownership Transfer),即auto_ptr 赋值给另一个 auto_ptr 时,原指针会失效,导致意外的 nullptr 访问。

示例:auto_ptr 的所有权转移

#include <iostream>
#include <memory>

int main() {
    std::auto_ptr<int> p1(new int(10));
    std::auto_ptr<int> p2 = p1;  // ✅ 所有权转移,p1 变成 nullptr

    std::cout << *p2 << std::endl;  // ✅ 正常输出 10
    std::cout << *p1 << std::endl;  // ❌ 未定义行为,p1 现在是 nullptr
}

📌 p1 在赋值后变成 nullptr,可能导致 nullptr 访问错误!


❌ 2. auto_ptr 不支持 shared_ptr 共享所有权

auto_ptr 中,对象的所有权只能属于一个 auto_ptr,无法在多个 auto_ptr 之间共享

std::auto_ptr<int> p1(new int(10));
std::auto_ptr<int> p2 = p1;  // ❌ p1 失效

📌 多个 auto_ptr 不能共享同一资源,容易导致资源丢失!
std::shared_ptr 允许多个智能指针共享同一资源,并使用引用计数管理对象的生命周期。


❌ 3. auto_ptr 不能用于 STL 容器

auto_ptr 不支持 STL 容器(std::vectorstd::list,因为STL 需要拷贝元素,而 auto_ptr 在拷贝时会转移所有权,导致数据丢失

🚨 示例

#include <vector>
#include <memory>

int main() {
    std::vector<std::auto_ptr<int>> vec;
    vec.push_back(std::auto_ptr<int>(new int(10)));  // ❌ 编译错误
}

📌 std::vector 需要拷贝 auto_ptr,但拷贝时 auto_ptr 会转移所有权,导致 vector 内的指针变 nullptr

解决方案:使用 std::unique_ptr

std::vector<std::unique_ptr<int>> vec;
vec.push_back(std::unique_ptr<int>(new int(10)));  // ✅ 兼容 STL

❌ 4. auto_ptr 不能与 delete[] 兼容

auto_ptr 只适用于 new,不支持 new[],不能正确释放数组

std::auto_ptr<int[]> arr(new int[10]);  // ❌ 未定义行为,无法正确释放数组

📌 C++11 std::unique_ptr<int[]> 支持 delete[],避免 auto_ptr 的问题。


2. std::auto_ptr 的替代方案

C++11 提供了更安全的智能指针:

智能指针替代 auto_ptr 作用特点
std::unique_ptr独占所有权(替代 auto_ptr不能复制,只能移动,安全
std::shared_ptr多个指针共享对象引用计数,自动释放
std::weak_ptr防止循环引用shared_ptr 的弱引用

使用 std::unique_ptr 代替 auto_ptr

#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<int> p1(new int(10));
    std::unique_ptr<int> p2 = std::move(p1);  // ✅ 只能移动,p1 失效

    if (!p1) std::cout << "p1 is null\n";  // ✅ p1 为空,避免 `nullptr` 访问
    std::cout << *p2 << std::endl;  // ✅ 正常输出 10
}

📌 std::unique_ptr 使用 std::move() 进行所有权转移,避免 auto_ptr 的隐式行为。


3. 关键总结

特点auto_ptr(已淘汰)unique_ptr(推荐)
所有权转移✅ 赋值时自动转移(⚠️ 容易导致 nullptr必须 std::move() 明确转移
是否可复制不能复制(但可以隐式转移)不能复制(只能 std::move()
STL 兼容性不兼容 STL完全兼容 STL
支持 delete[]❌ 不能正确释放数组支持 std::unique_ptr<int[]>

🚀 C++11 之后,应该使用 std::unique_ptrstd::shared_ptr 代替 auto_ptr,避免不安全的所有权转移和兼容性问题! 🚀

/* Copyright © 2022-present, ZEEKR Inc. All rights reserved. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. / #ifndef APF_MESSAGE_H #define APF_MESSAGE_H #include #include #include <condition_variable> #include #include “ara/core/variant.h” #include #define DOUBLE_QUEUE_NORNAL 0 #define DOUBLE_QUEUE_LIMIT 1 #define DOUBLE_QUEUE_SIZE 2 #define DOUBLE_QUEUE_DEPTH 50 namespace apf { namespace osa { /* @brief block message queue. @details block message queue. @Error interrupt handling : none @Input conditions : none @Output conditions : none / template class BlockMessageQueue { enum class MsgQueueId {}; public: /* @brief : send message. @details : send message. @param [in] _msg: the message to be sent. @return : none. @note add user data msg to queue and notify worker thread @see none / void sendMessage(const Msg &_msg) { std::unique_lockstd::mutex lk(m_mutex); m_queue.push(_msg); m_cv.notify_one(); } /* @brief : send message. @details : send message. @param [in] _msg: the message to be sent. @return : none. @note add user data msg to queue and notify worker thread @see none */ void sendMessage(Msg &&_msg) { std::unique_lockstd::mutex lk(m_mutex); m_queue.emplace(std::move(_msg)); m_cv.notify_one(); } /** @brief : receive message. @details : receive message. @param [out] : _msg: received message @return : return true if receive message success, otherwise return false @note Wait for a message to be added to the queue @see none */ bool receiveMessage(Msg &_msg) { std::unique_lockstd::mutex lk(m_mutex); @brief message queue. @details message queue. @Error interrupt handling : none @Input conditions : none @Output conditions : none */ template class MessageQueue { enum class MsgQueueId {}; public: using queue_prop_t = std::array<std::shared_ptr, DOUBLE_QUEUE_SIZE>; /** * @brief : send message. * @details : send message. * @param [in] std::shared_ptr _msg: the message to be sent. * @return : none. * @note add user data msg to queue and notify worker thread * @see none / void sendMessage(std::shared_ptr _msg) { std::unique_lockstd::mutex lk(m_mutex); if (m_queue.empty()) { m_queue.push(_msg); m_cv.notify_one(); } else { m_queue.push(_msg); } } /* * @brief : send limit queue message. * @details : send message. * @param [in] _msg: the message to be sent. * @return : none. * @note add user data msg to queue and notify worker thread * @see none / void sendLimitMessage(std::shared_ptr _msg) { std::unique_lockstd::mutex lk(m_mutex); if (m_limitQueue.size() >= DOUBLE_QUEUE_DEPTH) { m_limitQueue.push(_msg); m_limitQueue.pop(); return; } if (m_limitQueue.empty()) { m_limitQueue.push(_msg); m_cv.notify_one(); } else { m_limitQueue.push(_msg); } } /* * @brief : receive message. * @details : receive message. * @param [in] : none. * @return : std::array<std::shared_ptr, 2>, received message. * @note Wait for a message to be added to the queue * @see none / queue_prop_t recievdMessage() { std::unique_lockstd::mutex lk(m_mutex); queue_prop_t msg {nullptr, nullptr}; m_cv.wait(lk, & {return (!m_queue.empty() || !m_limitQueue.empty() || !m_isRunning);}); if (m_queue.empty() && m_limitQueue.empty()) { return msg; } if (!m_queue.empty()) { msg[DOUBLE_QUEUE_NORNAL] = m_queue.front(); m_queue.pop(); } if (!m_limitQueue.empty()) { msg[DOUBLE_QUEUE_LIMIT] = m_limitQueue.front(); m_limitQueue.pop(); } return msg; } /* * @brief : stop message queue. * @details : stop message queue. * @param [in] : none. * @return : none. * @note none * @see none */ void threadEndNotify() { m_isRunning = false; std::unique_lockstd::mutex lk(m_mutex); m_cv.notify_one(); } private: std::mutex m_mutex; /< mutex */ std::condition_variable m_cv; /< condition variables that control message reception */ std::queue<std::shared_ptr> m_queue; /< message queue */ std::queue<std::shared_ptr> m_limitQueue; /< message limit queue */ bool m_isRunning = true; /*< message queue state / }; / @brief message sender. @details message sender. @Error interrupt handling : none @Input conditions : none @Output conditions : none / template class MessageSender { public: /* @brief : MessageSender constructor. @details : MessageSender constructor. @param [in] MessageQueue & _msg_queue: message queue. @return : none. @note none @see none / MessageSender(MessageQueue & _msg_queue) :m_message_queue_proxy(&_msg_queue) {} /* @brief : constructor. @details : constructor. @param [in] MessageQueue & _msg_queue: message queue. @return : none. @note none @see none / MessageSender() :m_message_queue_proxy(nullptr) {} /* @brief : send message. @details : send message. @param [in] std::shared_ptr _msg: the message to be sent. @return : none. @note add user data msg to queue and notify worker thread @see MessageQueue::sendMessage() / void sendMessage(std::shared_ptr _msg) { if (m_message_queue_proxy) { m_message_queue_proxy->sendMessage(_msg); } } /* @brief : send sendLimitMessage. @details : send sendLimitMessage. @param [in] std::shared_ptr _msg: the message to be sent. @return : none. @note add user data msg to queue and notify worker thread with queue limit @see MessageQueue::sendLimitMessage() */ void sendLimitMessage(std::shared_ptr _msg) { if (m_message_queue_proxy) { m_message_queue_proxy->sendLimitMessage(_msg); } } private: MessageQueue *m_message_queue_proxy; /**< message queue proxy */ }; /// @brief template <typename MsgType, typename …BodyArgs> class MessageBase final { public: using Type = MsgType; //MessageBase() = default; //template <typename T, typename… Args> //static MessageBase& CreateMessage(Args… _args) { //// emplaceMsgBody(std::forward(_args)…); // return *this; //} template static std::shared_ptr CreateMessageByCopy(T const & _right_obj) { auto msg = std::make_shared(); msg->template emplaceMsgBodyByCopy(_right_obj); return msg; } template <typename T, typename… Args> static std::shared_ptr CreateMessage(Args&&… _args) { auto msg = std::make_shared(); msg->template emplaceMsgBody(std::forward(_args)…); return msg; } template T const& getMessageBody() const { return ara::core::get(m_msg_body); } MsgType getMessageType() const { return m_msg_type; } template <typename T, typename… Args> void emplaceMsgBody(Args&&… _args) { m_msg_body.template emplace(std::forward(_args)…); m_msg_type = T::m_s_message_type; } template void emplaceMsgBodyByCopy(T const & _right_obj) { m_msg_body.template emplace(_right_obj); m_msg_type = T::m_s_message_type; } private: MsgType m_msg_type{0}; ara::core::Variant<BodyArgs…> m_msg_body; }; } } #endif 需要画在原队列sendMessage的基础上新加sendLimitMessage队列相关处理。也要体现队列空后,进入wait后使用notifyonce再次触发线程的原理
最新发布
08-08
<think>我们有一个MessageQueue类,它有两个队列:普通队列m_queue和限制队列m_limitQueue。 在原有sendMessage方法的基础上,新增了sendLimitMessage方法用于向限制队列发送消息。 限制队列有一个最大深度DOUBLE_QUEUE_DEPTH(50),当超过这个深度时,新消息加入的同时会丢弃队列头部的消息(即最老的消息)。 接收消息的方法recievdMessage会同时检查两个队列,当任意队列不为空时,就会取出消息(如果两个队列都有消息,则同时取出两个队列的消息,返回一个数组,数组的第一个元素是普通队列的消息,第二个是限制队列的消息,如果某个队列为空则对应位置为nullptr)。 当两个队列都为空时,接收线程会通过条件变量m_cv等待,直到有消息到来或者线程被通知结束(m_isRunning置为false)。 现在需要画出在sendMessage的基础上新增sendLimitMessage队列的处理流程,并体现当队列空后进入wait,然后通过notify_once(实际上是notify_one)再次触发的原理。 我们可以用序列图来描述这个过程,因为序列图可以展示多个对象之间的交互顺序,特别适合展示消息的发送和接收过程。 由于原代码中有两个发送方法(sendMessage和sendLimitMessage)和一个接收方法(recievdMessage),我们考虑以下场景: 1. 初始状态两个队列都为空,接收线程在recievdMessage中等待(调用m_cv.wait)。 2. 发送线程调用sendMessage发送一个普通消息,此时会调用m_cv.notify_one()唤醒一个等待的线程。 3. 接收线程被唤醒,从普通队列中取出消息,然后返回。 4. 再次发送一个限制消息(sendLimitMessage),同样会调用m_cv.notify_one()(如果之前队列为空,则调用notify_one,否则只加入队列)。 5. 接收线程再次被唤醒,取出限制消息。 同时,我们还需要展示限制队列满的情况:当限制队列已满,再发送限制消息时,会先加入新消息,然后丢弃最老的消息(pop_front),此时不会调用notify_one,因为队列本来就不为空。 但是,由于我们需要在一张图中展示多个场景,可能比较复杂。因此,我们可以分步骤画图,或者用注释说明。 然而,题目要求画在原队列sendMessage的基础上新加sendLimitMessage队列相关处理,也要体现队列空后,进入wait后使用notify_one再次触发线程的原理。 我们可以这样设计序列图: 参与者:发送线程(SenderThread)、接收线程(ReceiverThread)、普通队列(NormalQueue)、限制队列(LimitQueue)、条件变量(ConditionVariable) 步骤: 1. 接收线程调用recievdMessage,检查两个队列都为空,于是调用条件变量的wait,进入等待。 2. 发送线程调用sendMessage(发送普通消息): a. 获取互斥锁 b. 将消息加入普通队列 c. 由于之前队列为空(所以需要唤醒),调用条件变量的notify_one d. 释放互斥锁 3. 条件变量唤醒等待的接收线程(随机一个,这里只有一个) 4. 接收线程被唤醒,再次检查队列,发现普通队列有消息,于是取出消息,然后返回(释放锁) 5. 接收线程再次进入recievdMessage,此时队列为空,再次进入wait 6. 发送线程调用sendLimitMessage(发送限制消息): a. 获取互斥锁 b. 检查限制队列大小,假设未满(比如第一次发送),则加入队列,并且因为之前队列为空,所以调用notify_one c. 释放互斥锁 7. 条件变量再次唤醒接收线程 8. 接收线程被唤醒,检查到限制队列有消息,取出消息,返回 另外,对于限制队列满的情况: 9. 发送线程连续发送51条限制消息(使队列满,然后再发一条): a. 获取锁 b. 发现队列已满(size>=50),则先push再pop(这里注意,代码中是先push,然后检查如果超过深度,就pop。但实际代码中,在sendLimitMessage中,如果队列大小已经>=50,则先push然后pop,这样队列大小不变。注意:代码中先push后pop,所以队列大小会先变成51,然后pop又变成50。但这样会导致队列中始终是50个,新来的消息会挤掉最老的消息。 c. 由于队列在加入前就不为空,所以不会调用notify_one(因为代码中,只有队列在加入前为空时,才调用notify_one,否则只是加入队列) d. 释放锁 但是,由于接收线程可能正在处理,所以不会影响接收线程,只是队列中始终保留最新的50条。 由于序列图较长,我们只画前面8步,即正常情况下的交互,并在图中标注限制队列满的情况。 但是,题目要求用PlantUML,并且之前都是组件图,现在要画序列图。 因此,我们绘制序列图如下:</think>### 消息队列系统优化设计解析 #### 双队列架构实现原理 ```plantuml @startuml !theme plain title "双队列消息处理序列图" participant "发送线程" as Sender participant "消息队列" as Queue participant "接收线程" as Receiver participant "条件变量" as CV group 初始状态 Receiver -> Queue: recievdMessage() Queue -> CV: wait() [队列空] end group 普通消息发送 Sender -> Queue: sendMessage(msg1) alt 队列为空 Queue -> CV: notify_one() end CV -> Receiver: 唤醒 Receiver -> Queue: 获取msg1 end group 限制队列消息发送 Sender -> Queue: sendLimitMessage(msg2) alt 队列满(>=50) Queue -> Queue: pop_front()\nLRU淘汰 else alt 队列为空 Queue -> CV: notify_one() end end end group 消息处理循环 Receiver -> Queue: 处理msg1 Receiver -> Queue: 处理msg2 Receiver -> Queue: recievdMessage() Queue -> CV: wait() [再次等待] end @enduml ``` #### 关键代码实现优化 1. **双队列管理核心逻辑**: ```cpp void sendMessage(std::shared_ptr<Msg> _msg) { std::unique_lock<std::mutex> lk(m_mutex); if (m_queue.empty()) { // 队列空时唤醒 m_cv.notify_one(); } m_queue.push(_msg); } void sendLimitMessage(std::shared_ptr<Msg> _msg) { std::unique_lock<std::mutex> lk(m_mutex); if (m_limitQueue.size() >= DOUBLE_QUEUE_DEPTH) { m_limitQueue.pop(); // LRU淘汰 } if (m_limitQueue.empty()) { // 队列空时唤醒 m_cv.notify_one(); } m_limitQueue.push(_msg); } ``` 2. **智能唤醒接收线程**: ```cpp queue_prop_t recievdMessage() { std::unique_lock<std::mutex> lk(m_mutex); // 仅当双队列均空时才等待 m_cv.wait(lk, [&] { return !(m_queue.empty() && m_limitQueue.empty()) || !m_isRunning; }); queue_prop_t msg{nullptr, nullptr}; if (!m_queue.empty()) { msg[DOUBLE_QUEUE_NORNAL] = m_queue.front(); m_queue.pop(); } if (!m_limitQueue.empty()) { msg[DOUBLE_QUEUE_LIMIT] = m_limitQueue.front(); m_limitQueue.pop(); } return msg; } ``` #### 系统工作原理流程图 ```mermaid graph TD A[消息到达] --> B{消息类型} B -->|普通消息| C[sendMessage] B -->|关键消息| D[sendLimitMessage] C --> E{普通队列空?} E -->|是| F[触发notify_one] E -->|否| G[直接入队] D --> H{限制队列满?} H -->|是| I[淘汰最老消息] H -->|否| J{队列空?} J -->|是| K[触发notify_one] J -->|否| L[直接入队] M[接收线程] --> N{双队列空?} N -->|是| O[条件变量等待] N -->|否| P[并行处理消息] O -->|notify_one唤醒| P ``` #### 设计优势分析 1. **智能唤醒机制**: - 仅在队列状态转换时触发通知 - 避免虚假唤醒导致的CPU空转 - 条件变量与队列状态严格同步 2. **LRU淘汰策略优化**: ```cpp // 淘汰算法时间复杂度O(1) if (m_limitQueue.size() >= DOUBLE_QUEUE_DEPTH) { m_limitQueue.pop(); // 淘汰最老元素 } ``` 3. **线程安全保证**: - 互斥锁保护所有队列操作 - RAII模式管理锁生命周期 - 条件变量wait()自动释放锁 4. **优雅停机处理**: ```cpp void threadEndNotify() { m_isRunning = false; std::unique_lock<std::mutex> lk(m_mutex); m_cv.notify_one(); // 唤醒所有等待线程 } ``` > 该设计特别适合自动驾驶等实时系统,其中普通队列用于传感器数据(可容忍丢失),限制队列用于关键控制指令(必须处理最新指令)。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值