I'm trying to understand the thread safety issue in the following construction:
Thread1()
{
forever
{
data.modify();
Thread2.takeThis(data);
msleep(20);
}
}
Thread2()
{
takeThis(data)
{
this->data = data;
}
forever
{
qDebug() << data;
msleep(20);
}
}
Thread1()
{
QHash data;
forever
{
data.modify();
Thread2.takeThis(data);
msleep(20);
}
}
Thread2()
{
QHash data;
takeThis(data)
{
this->data = data;
}
forever
{
qDebug() << data;
msleep(20);
}
}
To copy to clipboard, switch view to plain text mode
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:
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);
}
}
Thread1()
{
QHash data;
forever
{
data.modify();
Thread2.takeThis(data);
msleep(20);
}
}
Thread2()
{
QHash data;
bool reading = false;
takeThis(data)
{
if (!reading)
this->data = data;
}
forever
{
reading = true;
qDebug() << data;
reading = false;
msleep(20);
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks