Thanks. So in other words, where this pattern is safe for primitives and non-COW objects:
int valueForThread;
void threadFunction () {
// do stuff with valueForThread
}
void mainFunction () {
int value = ...;
valueForThread = value; // create a copy for thread
startThread(&threadFunction);
value = ...; // doesn't affect thread
}
int valueForThread;
void threadFunction () {
// do stuff with valueForThread
}
void mainFunction () {
int value = ...;
valueForThread = value; // create a copy for thread
startThread(&threadFunction);
value = ...; // doesn't affect thread
}
To copy to clipboard, switch view to plain text mode
This would *not* be OK for a QVector:
QVector<int> valueForThread;
void threadFunction () {
// do stuff with valueForThread
}
void mainFunction () {
QVector<int> value = ...;
valueForThread = value; // create a copy for thread
startThread(&threadFunction);
value = ...; // doesn't affect thread
}
QVector<int> valueForThread;
void threadFunction () {
// do stuff with valueForThread
}
void mainFunction () {
QVector<int> value = ...;
valueForThread = value; // create a copy for thread
startThread(&threadFunction);
value = ...; // doesn't affect thread
}
To copy to clipboard, switch view to plain text mode
And therefore I need to review the documentation for a Qt object carefully before using it in this way to make sure no behind-the-scenes COW could be happening as a result and, if it is, synchronize accordingly. Correct?
So in the above example I would need to protect 'valueForThread' with a mutex but *also* acquire the mutex when modifying 'value' in mainFunction()?
Does that also mean an alternative is to somehow force a deep copy when doing 'valueForThread = value' so that I don't run into the problem at all and the copies become completely independent?
Thanks.
Bookmarks