7

我偶然发现了一种似乎存在于所有数据对象中的方法,例如QList, QQueue, QHash...

我什至调查到目前为止我可以看到它的源代码,即

inline void setSharable(bool sharable) {
    if (!sharable) detach(); d->sharable = sharable;
}

qlist.h中(第 117 行)。

但它对QList, QQueue, QHash... 有什么影响?它是否与线程相关(这听起来很合理)?

感谢您的任何回答,请仅在您有实际知识的情况下回答。

4

2 回答 2

6

没有人能说得更清楚了:

http://qt.nokia.com/doc/4.6/implicit-sharing.html

以这种方式实现容器是常见的做法。

于 2010-03-27T20:45:12.817 回答
5

您询问的可共享状态与多线程无关。相反,它是分发对内部状态的引用的写时复制数据类(甚至是单线程的)的实现细节。

考虑一个String使用 CoW 实现的类(为了说明的目的,这个类在线程上下文中不可用,因为访问d->refcount不同步,它也不能确保内部char数组以 结尾'\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;
    }

以及每个用于 mutating 和 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];
    }

};

到现在为止还挺好。但是如果我们想提供一个 mutableoperator[]怎么办?

    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];
    }

这种幼稚的实现有一个缺陷。考虑以下场景:

    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!

为了防止这种情况,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];
    }

什么时候重新设置shareable状态true?一种常见的技术是说,在调用非常量方法时,对内部状态的引用无效,所以这shareable就是重置回true. 由于每个非常量函数都调用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;
    }
于 2012-06-19T20:08:39.533 回答