shared_ptr的 - 按值传递VS按引用传递 [英] shared_ptr - pass by value vs pass by reference

查看:235
本文介绍了shared_ptr的 - 按值传递VS按引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

 的typedef的boost :: shared_ptr的<事件> EventPtr;

在一个线程中,我创建一个事件并发送其关闭,以获得出动:

 事件*事件=新事件();
EventPtr eventPtr(事件);
EventDispatcher受::讯(eventPtr); //伪code

的EventDispatcher 收到EventPtr并把它添加到被另一个线程处理的队列......但什么是调度方法的适当方法签名?

 讯(EventPtr事件); //会的push_back(事件)

 调度(常量EventPtr&安培;事件); //会的push_back(事件);

考虑到我的EventDispatcher有一个队列:

 的typedef的std ::队列< EventPtr>的EventQueue
EventQueue的theQueue;

然后以后,其他线程弹出从队列并把它一个事件关的东西来处理该事件

  EventPtr事件= theQueue.front();
EventProcessor ​​::过程(事件); //伪code
theQueue.pop();

再次什么是过程方法适当的方法签名?我想知道如果我可以通过肉眼事件* 来的工艺方法?

我想我不知道我应该只是按值传递所以引用计数是准确的?我真的只关注一个事实,即一个线程推到队列中,另一个线程突然离开队列,我不会去什么地方漏球...

谢谢!


解决方案

  

向EventDispatcher收到EventPtr并把它添加到被另一个线程处理的队列......但什么是调度方法的适当方法签名?


要么建议是罚款;路过const引用很可能会更有效,因为它不会有修改指针的引用计数。在这两种情况下,的push_back 将放置指针的拷贝队列,保持活着的事件,而它的队列中。


  

此外,什么是工艺方法的适当方法签名?我想知道如果我可以通过肉眼事件*的工艺方法?


传递共享的指针(按值或引用)将清楚地记录和执行活动的所有权,并让处理器保持它保持调用进程()是否需要。传递一个原始指针不确定性引入到所有权;处理器将需要一个合同,说明它没有采取事件的所有权,而不能尝试一次访问进程()已经结束。

Suppose I have:

typedef boost::shared_ptr<Event> EventPtr;

On one thread, I am creating an Event and sending it off to get dispatched:

Event* event = new Event();
EventPtr eventPtr(event);
EventDispatcher::dispatch(eventPtr);  //pseudocode

The EventDispatcher receives an EventPtr and adds it to a queue that gets processed in another thread...but what is an appropriate method signature for the dispatch method?

dispatch(EventPtr event);  //will push_back(event)

or

dispatch(const EventPtr& event);  //will push_back(event);

Considering that my EventDispatcher has a queue:

typedef std::queue<EventPtr> EventQueue
EventQueue theQueue;

Then later, the other thread pops an Event off the queue and hands it off to something to process the event:

EventPtr event = theQueue.front();
EventProcessor::process(event);  //pseudocode
theQueue.pop();

Again, what is an appropriate method signature for the process method? I'm wondering if I can just pass the naked Event* to the process method?

I guess I'm wondering am I supposed to just pass by value so the reference count is accurate? I'm really only concerned about the fact that one thread is pushing onto the queue and another thread is popping off the queue and I'm not going to leak pointers somewhere...

Thanks!

解决方案

The EventDispatcher receives an EventPtr and adds it to a queue that gets processed in another thread...but what is an appropriate method signature for the dispatch method?

Either suggestion is fine; passing by const reference will likely be more efficient, since it won't have to modify the pointer's reference count. In either case, push_back will place a copy of the pointer on the queue, keeping the event alive while it's on the queue.

Again, what is an appropriate method signature for the process method? I'm wondering if I can just pass the naked Event* to the process method?

Passing the shared pointer (by value or reference) will clearly document and enforce the ownership of the event, and will allow the processor to keep hold of it after the call to process() if it needs to. Passing a raw pointer introduces uncertainty into the ownership; the processor will need a contract stating that it isn't taking ownership of the event, and must not attempt to access it once process() has ended.

这篇关于shared_ptr的 - 按值传递VS按引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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