什么是隐式共享? [英] What is implicit sharing?

查看:234
本文介绍了什么是隐式共享?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++建立一个游戏引擎库。稍后,我使用Qt构建一个应用程序,并相当着迷于使用隐式共享 。我想知道是否有人可以更详细地解释这个技巧,或者提供一个简单的例子。

I am building a game engine library in C++. A little while back I was using Qt to build an application and was rather fascinated with its use of Implicit Sharing. I am wondering if anybody could explain this technique in greater detail or could offer a simple example of this in action.

推荐答案

隐含共享背后的想法似乎使用更常见的术语 copy-on-write 。 copy-on-write背后的想法是让每个对象用作指向实际实现的指针的包装器。每个实现对象跟踪其中的指针数。每当对包装器对象执行操作时,它只是被转发到实现对象,这实际上是工作。

The key idea behind implicit sharing seems to go around using the more common term copy-on-write. The idea behind copy-on-write is to have each object serve as a wrapper around a pointer to the actual implementation. Each implementation object keeps track of the number of pointers into it. Whenever an operation is performed on the wrapper object, it's just forwarded to the implementation object, which does the actual work.

这种方法的优点是复制和销毁这些对象都很便宜。要创建一个对象的副本,我们只是创建一个包装器的新实例,设置它的指针指向实现对象,然后增加指向对象的指针数量的计数(这有时称为参考计数,顺便说一下)。破坏是类似的 - 我们将引用计数减1,然后看看是否有其他人指向实现。如果没有,我们释放它的资源。

The advantage of this approach is that copying and destruction of these objects are cheap. To make a copy of the object, we just make a new instance of a wrapper, set its pointer to point at the implementation object, and then increment the count of the number of pointers to the object (this is sometimes called the reference count, by the way). Destruction is similar - we drop the reference count by one, then see if anyone else is pointing at the implementation. If not, we free its resources. Otherwise, we do nothing and just assume someone else will do the cleanup later.

这种方法的挑战是,这意味着多个不同的对象都将指向同样的实现。这意味着如果有人最终对实现进行更改,引用该实现的每个对象都将看到更改 - 这是一个非常严重的问题。为了解决这个问题,每次执行可能改变实现的操作时,操作通过查看引用计数是否相同来检查是否有任何其他对象也引用该实现。如果没有其他对象引用该对象,则操作可以直接进行 - 没有可能的变化传播。如果有至少一个其他对象引用数据,则包装器首先对自身的实现进行深度复制,并将其指针更改为指向新对象。

The challenge in this approach is that it means that multiple different objects will all be pointing at the same implementation. This means that if someone ends up making a change to the implementation, every object referencing that implementation will see the changes - a very serious problem. To fix this, every time an operation is performed that might potentially change the implementation, the operation checks to see if any other objects also reference the implementation by seeing if the reference count is identically 1. If no other objects reference the object, then the operation can just go ahead - there's no possibility of the changes propagating. If there is at least one other object referencing the data, then the wrapper first makes a deep-copy of the implementation for itself and changes its pointer to point to the new object. Now we know there can't be any sharing, and the changes can be made without a hassle.

如果你想看到一些实际的例子,请参阅斯坦福大学介绍C ++编程课程中的演示示例15.0和16.0。它显示了如何使用这种技术设计一个对象来保存单词列表。

If you'd like to see some examples of this in action, take a look at lecture examples 15.0 and 16.0 from Stanford's introductory C++ programming course. It shows how to design an object to hold a list of words using this technique.

希望这有助于!

这篇关于什么是隐式共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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