shared_ptr现实生活中的用例 [英] shared_ptr Real life use-cases

查看:57
本文介绍了shared_ptr现实生活中的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shared_ptr用于我们希望有一个动态分配项目的多个所有者的场景.

shared_ptr is to be used when we have a scenario where it is desirable to have multiple owners of a dynamically allocated item.

问题是,我无法想象需要多个所有者的任何情况.我可以描绘的每个用例都可以使用unique_ptr来解决.

Problem is, I can't imagine any scenario where we require multiple owners. Every use-case I can image can be solved with a unique_ptr.

有人可以提供一个实际的用例示例,其中包含shared_ptr是必需的代码(按要求,我的意思是作为智能指针的最佳选择)?所谓现实生活",是指一些实际且务实的用例,而不是过于抽象和虚构的用例.

Could someone provide a real life use-case example with code where shared_ptr is required (and by required, I mean the optimal choice as a smart pointer)? And by "real life" I mean some practical and pragmatic use-case, not something overly abstract and fictitious.

推荐答案

在我们的模拟器产品中,我们使用框架在模拟组件(称为端点)之间传递消息.这些端点可以驻留在进程中的多个线程上,甚至可以驻留在模拟群集中的多台计算机上,这些消息中的消息是通过RDMA或TCP连接的网格路由的.该API大致如下:

In our simulator product, we use a framework to deliver messages between simulation components (called endpoints). These endpoints could reside on multiple threads within a process, or even on multiple machines in a simulation cluster with messages routed through a mesh of RDMA or TCP connections. The API looks roughly like:

class Endpoint {
public:
    // Fill in sender address, etc., in msg, then send it to all
    // subscribers on topic.
    void send(std::unique_ptr<Message> msg, TopicId topic);

    // Register this endpoint as a subscriber to topic, with handler
    // called on receiving messages on that topic.
    void subscribe(TopicId topic,
        std::function<void(std::shared_ptr<const Message>)> handler);
};

通常,一旦发送方端点执行了 send ,它就不必等待来自任何接收方的任何响应.因此,如果我们试图在整个消息路由过程中保留一个所有者,那么将所有权保留在 send 或其他情况下, send <他们必须等到所有接收者都完成了对消息的处理之后,这才会引入不必要的往返延迟.另一方面,如果多个接收者都订阅了 topic ,则为每个接收者分配唯一所有权也没有意义,因为我们不知道接收者需要哪个消息最长.这将使路由基础结构本身成为唯一所有者.但是,在那种情况下,路由基础设施将不得不等待所有接收者完成,而基础设施可能具有大量要传递给多个线程的消息,并且它还希望能够将消息传递给接收者和接收者.能够转到要发送的下一条消息.另一种选择是保留一组唯一的指向发送消息的指针,以等待线程对其进行处理,并让接收者在完成消息后通知消息路由器.但这也会带来不必要的开销.

In general, once the sender endpoint has executed send, it does not need to wait for any response from any receiver. So, if we were to try to keep a single owner throughout the message routing process, it would not make sense to keep ownership in the caller of send or otherwise, send would have to wait until all receivers are done processing the message, which would introduce an unnecessary round trip delay. On the other hand, if multiple receivers are subscribed to the topic, it also wouldn't make sense to assign unique ownership to any single one of them, as we don't know which one of them needs the message the longest. That would leave the routing infrastructure itself as the unique owner; but again, in that case, then the routing infrastructure would have to wait for all receivers to be done, while the infrastructure could have numerous messages to deliver to multiple threads, and it also wants to be able to pass off the message to receivers and be able to go to the next message to be delivered. Another alternative would be to keep a set of unique pointers to messages sent waiting for threads to process them, and have the receivers notify the message router when they're done; but that would also introduce unnecessary overhead.

另一方面,通过在此处使用 shared_ptr ,一旦路由基础结构完成了将消息传递到端点的传入队列的操作,便可以释放所有权以在各个接收者之间共享.然后,线程安全的引用计数确保一旦所有接收者完成对 Message 的处理后,就释放它们.而且在远程计算机上有订阅者的情况下,序列化和传输组件可以在消息工作时成为消息的另一个共享所有者.然后,在接收机器上,接收和反序列化组件可以将其创建的 Message 副本的所有权转给该机器上的接收者的共享所有权.

On the other hand, by using shared_ptr here, once the routing infrastructure is done delivering messages to incoming queues of the endpoints, it can then release ownership to be shared between the various receivers. Then, the thread-safe reference counting ensures that the Message gets freed once all the receivers are done processing it. And in the case that there are subscribers on remote machines, the serialization and transmission component could be another shared owner of the message while it's doing its work; then, on the receiving machine(s), the receiving and deserialization component can pass off ownership of the Message copy it creates to shared ownership of the receivers on that machine.

这篇关于shared_ptr现实生活中的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆