As noted in the comment, you are probably trying to write the same data from different threads. I am writing "probably" only because you have not shared the usage of your QThread
subclass.
As pointed out in the comment, not even an assignment is guaranteed by the C++ standard to be thread-safe for classes like your Object
subclass with a QString
inside.
You could use std::atomic
as per hint, although it has only been available since C++11. A more cross-solution would be to use QMutex
for your scenario, or probably evn better to use the RAII solution with QMutexLocker
which will take care of the unlocking for you, automatically. So you would be something like the code below.
class Thr : public QThread{
public:
My* my;
QMutex mutex; // <--- first addition
protected:
void run(){
while (true){
QString copy = my->foo;
QString bar = copy.toUpper();
QMutexLocker locker(&mutex); // <--- second addition
my->foo = bar.toLower();
}
}
};
Of course, the recommended solution depends on further factors, for instance, you could also redesign your program not to use a pointer depending on the exact use case at hand, et al.