dra*_*hnr 6 c++ qt copy-on-write qlist
我迷迷糊糊关于这似乎是存在于所有数据的方法对象,如QList
,QQueue
,QHash
...
我甚至调查到目前为止我可以看到它的源代码,这是
inline void setSharable(bool sharable) {
if (!sharable) detach(); d->sharable = sharable;
}
Run Code Online (Sandbox Code Playgroud)
在qlist.h中(第117行).
但是,它有什么样的影响上QList
,QQueue
,QHash
...?它是否与线程有关(听起来合理)?
感谢您的回答,如果您有实际知识,请回答.
你问的可共享状态与多线程无关.相反,它是写入时复制数据类(甚至是单线程数据类)的实现细节,它们分发对内部状态的引用.
考虑String
使用CoW实现的类(为了便于说明,此类在线程上下文中不可用,因为访问d->refcount
不同步,它也不能确保内部char
arrary结束'\0'
,并且可能会吃掉你的祖母;你被警告过):
struct StringRep {
StringRep()
: capacity(0), size(0), refcount(0), sharable(true), data(0) {}
~StringRep() { delete[] data; }
size_t capacity, size, refcount;
bool sharable; // later...
char * data;
};
class String {
StringRep * d;
public:
String() : d(new StringRep) { ++d->refcount; }
~String() { if (--d->refcount <= 0) delete d; }
explicit String(const char * s)
: d(new StringRep)
{
++d->refcount;
d->size = d->capacity = strlen(s);
d->data = new char[d->size];
memcpy(d->data, s, d->size);
}
String(const String &other)
: d(other.d)
{
++d->refcount;
}
void swap(String &other) { std::swap(d, other.d); }
String &operator=(const String &other) {
String(other).swap(*this); // copy-swap trick
return *this;
}
Run Code Online (Sandbox Code Playgroud)
并且每个用于变异和const方法的示例函数:
void detach() {
if (d->refcount == 1)
return;
StringRep * newRep = new StringRep(*d);
++newRep->refcount;
newRep->data = new char[d->size];
memcpy(newRep->data, d->data, d->size);
--d->refcount;
d = newRep;
}
void resize(size_t newSize) {
if (newSize == d->size)
return;
detach(); // mutator methods need to detach
if (newSize < d->size) {
d->size = newSize;
} else if (newSize > d->size) {
char * newData = new char[newSize];
memcpy(newData, d->data, d->size);
delete[] d->data;
d->data = newData;
}
}
char operator[](size_t idx) const {
// no detach() here, we're in a const method
return d->data[idx];
}
};
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是如果我们想提供一个可变的operator[]
呢?
char & operator[](size_t idx) {
detach(); // make sure we're not changing all the copies
// in case the returned reference is written to
return d->data[idx];
}
Run Code Online (Sandbox Code Playgroud)
这种天真的实现有一个缺陷.请考虑以下情形:
String s1("Hello World!");
char & W = s1[7]; // hold reference to the W
assert( W == 'W' );
const String s1(s2); // Shallow copy, but s1, s2 should now
// act independently
W = 'w'; // modify s1 _only_ (or so we think)
assert( W == 'w' ); // ok
assert( s1[7] == 'w' ); // ok
assert( s2[7] == 'W' ); // boom! s2[7] == 'w' instead!
Run Code Online (Sandbox Code Playgroud)
为了防止这种情况,String
在分发对内部数据的引用时必须将自身标记为不可共享,以便从中获取的任何副本总是很深.所以,我们需要调整detach()
并char & operator[]
喜欢这样:
void detach() {
if (d->refcount == 1 && /*new*/ d->sharable)
return;
// rest as above
}
char & operator[](size_t idx) {
detach();
d->shareable = false; // new
return d->data[idx];
}
Run Code Online (Sandbox Code Playgroud)
什么时候重新shareable
恢复状态true
?一种常见的技术是,在调用非const方法时,对内部状态的引用无效,因此shareable
重置为的位置true
.由于每个非const函数调用detach()
,我们可以shareable
在那里重置,因此detach()
最终变为:
void detach() {
if (d->refcount == 1 && d->sharable) {
d->sharable = true; // new
return;
}
d->sharable = true; // new
StringRep * newRep = new StringRep(*d);
++newRep->refcount;
newRep->data = new char[d->size+1];
memcpy(newRep->data, d->data, d->size+1);
--d->refcount;
d = newRep;
}
Run Code Online (Sandbox Code Playgroud)