Question about implicit sharing
I want to make sure I understand this right.
I have an implicitly shared container, say a QHash. Object A creates it and passes it to object B by value.
So far, when reading from the QHash both A and B would read from the same memory block, right?
Now A modifies the QHash.
Does the QHash get detached in this case? Or only when B modifies its own copy?
And if it does get detached, what happens when A modifies the QHash multiple times? Does it get detached every time?
Re: Question about implicit sharing
Quote:
Originally Posted by
Cruz
So far, when reading from the QHash both A and B would read from the same memory block, right?
Yes, exactly. There are multiple instances of QHash, but the internal data is shared between the instances.
Quote:
Now A modifies the QHash.
Does the QHash get detached in this case? Or only when B modifies its own copy?
Yes, it gets automatically detached when modifying and if the reference count is greater than one.
Quote:
And if it does get detached, what happens when A modifies the QHash multiple times? Does it get detached every time?
No, because after detaching in the first place the reference count is updated appropriately. From then on, it's the only occurrence using the modified data. There is no need to detach if the reference count is one.
Re: Question about implicit sharing
I'm trying to understand the thread safety issue in the following construction:
Code:
Thread1()
{
forever
{
data.modify();
Thread2.takeThis(data);
msleep(20);
}
}
Thread2()
{
takeThis(data)
{
this->data = data;
}
forever
{
qDebug() << data;
msleep(20);
}
}
If Thread1 happens to modify the data while Thread2 is qDebug()-ing it, then nothing bad happens, because the data is detached, right?
If above statement is true, then the problem must occur, when takeThis() of Thread2 overwrites it's own data while it's in the process of qDebug()-ing.
And if both statements are true, then I don't understand why the following construction still crashes:
Code:
Thread1()
{
forever
{
data.modify();
Thread2.takeThis(data);
msleep(20);
}
}
Thread2()
{
bool reading = false;
takeThis(data)
{
if (!reading)
this->data = data;
}
forever
{
reading = true;
qDebug() << data;
reading = false;
msleep(20);
}
}
I'm trying to find a solution, where Thread1 is not blocked by a mutex when trying to deliver its data, but rather doesn't deliver and continues to calculate the next data set.
Re: Question about implicit sharing
Re: Question about implicit sharing
Just note the difference between reentrancy and thread-safety.