共享内存和复制在写或右值引用和移动语义? [英] Shared memory and copy on write or rvalue references and move semantics?

查看:185
本文介绍了共享内存和复制在写或右值引用和移动语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是C ++ 11移动语义和右值引用取代的通用容器的写入实现的共享内存/副本(如Qt的容器中的)。

Is a shared memory/copy on write implementation for general containers (like that found in Qt's containers) superseded by C++11 move semantics and rvalue references?

一个失败,另一个失败?

Where does one fail and the other succeed? Or are they complementary rather than alternatives?

推荐答案

在写和移动语义上的副本都被用来优化对象的值语义保持他们的数据在堆上。 std :: string ,例如已经实现为写入时复制对象和移动启用对象。

Both copy on write and move semantics have been used to optimize value semantics of objects that hold their data on the heap. std::string, for example has been implemented both as a copy-on-write object, and as a move-enabled object.

所以copy-on-write和move语义在这方面是类似的:如果你足够宽松地定义copy,它们都可以用来优化副本。我有时将移动语义描述为写入时复制,引用计数限制为0或1,因此包含引用计数的字段被优化。

So copy-on-write and move semantics is similar in that regard: they can both be used to optimize "copies" if you define "copy" loosely enough. I've sometimes described move semantics as copy-on-write with the reference count being limited to 0 or 1, and thus the field containing the reference count is optimized away.

std :: lib中的所有容器现在都使用move语义,甚至 std :: string ,它们以前被允许使用写时拷贝,现在禁止这样做。如果我今天正在写一个新的客户容器,我会在选择copy-on-write之前使用move语义。

All of the containers in the std::lib now use move semantics and even std::string, which used to be allowed to use copy-on-write, is now forbidden from doing so. If I were writing a new customer container today, I would use move semantics before choosing copy-on-write.

仍然有一个用于copy-on-write C ++ 11。如果你期望你的数据结构很少被写入,但经常复制,许多客户端持有相同值的副本,写入时复制仍然是一个大胜利。

There is still a use for copy-on-write in C++11. If you expect your data structure to be rarely written to, but copied often, with many clients holding copies of the same value, copy-on-write can still be a large win.

例如,我看到copy-on-write可以很好地用于保存复杂文档的undo列表。在任何给定的提交(你想保存状态),只有一小块大文档自上次提交以来已更改。因此,复制文档以保存其状态意味着更新一堆引用计数,实际上将更改(写入时复制样式)为一小块。

For example I've seen copy-on-write put to good use to hold an undo-list for a complex document. At any given commit (where you want to save state), only a small piece of the large document has changed since the last commit. So making a copy of the document to save its state means updating a bunch of reference counts, and actually making changes (copy-on-write style) to a small piece.

这篇关于共享内存和复制在写或右值引用和移动语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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