STL中的写时复制支持 [英] Copy-on-write support in STL
问题描述
我刚刚在阅读维基百科上写的复制文章(好奇,如果有任何并支持它的文件系统),并惊讶于以下段落:
I was just reading a Wikipedia article on Copy-on-write (curious if there are any filesystems that support it), and was surprised by the following passage:
COW也在内核,库,应用程序和系统码。例如,C ++标准库提供的字符串类是专门设计用于允许写时复制实现的:
COW is also used outside the kernel, in library, application and system code. The string class provided by the C++ standard library, for example, was specifically designed to allow copy-on-write implementations:
std::string x("Hello");
std::string y = x; // x and y use the same buffer
y += ", World!"; // now y uses a different buffer
// x still uses the same old buffer
我不知道STL中每次都支持写时复制。真的吗?它适用于其他STL类,例如。 std :: vector
或 std :: array
?哪些编译器支持该优化(特别是,我不知道G ++,Intel C ++编译器和Microsoft C ++编译器)?
I didn't know that copy-on-write was every supported in STL. Is that true? Does it apply to other STL classes, e.g. std::vector
or std::array
? Which compilers support that optimization (in particular, I wonder about G++, Intel C++ compiler and Microsoft C++ compiler)?
推荐答案
例如,C ++标准库提供的字符串类专门设计为允许写时复制实现。
The string class provided by the C++ standard library, for example, was specifically designed to allow copy-on-write implementations
<这是半真值。是的,它开始设计与COW在心。但在急忙的std :: string的公共接口被搞砸了。导致它得到COW敌意。问题是在标准发布后发现的,我们从那以后就坚持了。作为立场目前 std :: string
不能是线程安全的COW编辑和在野外的实现不做它。
That is half-truth. Yes, it started design with COW in mind. But in the rush the public interface of std::string was messed up. Resulting it getting COW-hostile. The problems were discovered after the standard published, and we're stuck with that ever since. As stands currently std::string
can not be thread-safely COW-ed and implementations in the wild don't do it.
如果您想要使用COW使用字符串,请从另一个库中获取它,例如MFC / ATL中的CString。
If you want a COW-using string, get it from another library, like CString in MFC/ATL.
这篇关于STL中的写时复制支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!