Does shared memory access from different QThreads behave similarly to native python threads?
-
wrote on 11 Jan 2021, 17:06 last edited by
Hi. I don't really have experience with threads and I'm currently trying to separate some parts of a PySide based software into different threads.
AFAIK, a standardthreading.Thread
object created in the main thread shares the memory with the main thread which enables direct access and modification of data from both sides. Just to make sure, I want to ask, if this still holds for QThreads in all cases.
If I instanciate some objecta
of classA(QObject)
in a custom QThread, for instance, could I savely access and modify data (attributes) ofa
from the main thread (assuming I got a reference toa
through a signal or sth)?
Thanks! -
Hi,
Unless the class is specifically designed to be thread safe, it's never safe to access them without proper protection in place.
-
Hi,
Unless the class is specifically designed to be thread safe, it's never safe to access them without proper protection in place.
wrote on 12 Jan 2021, 08:47 last edited by nutrx 1 Dec 2021, 08:49@SGaist Ah yes, if I have more threads as in the example above that access the same data, I need to make sure that this does not happen at the same time. In this case I would need to add some locking mechanism probably (never did this but I have about it, it shouldn't be an issue in my application). Is this what you mean?
I was just wondering if there is some conceptual restriction so that it's always illegal to access data of another QThread directly. -
@SGaist Ah yes, if I have more threads as in the example above that access the same data, I need to make sure that this does not happen at the same time. In this case I would need to add some locking mechanism probably (never did this but I have about it, it shouldn't be an issue in my application). Is this what you mean?
I was just wondering if there is some conceptual restriction so that it's always illegal to access data of another QThread directly.wrote on 12 Jan 2021, 08:53 last edited by@nutrx
Yes, conceptually you are allowed to access the shared data across threads.From Qt you would typically use
QMutex
/QMutexLocker
to synchronise safe access.I don't know, but it may be that Python objects across threads has this protection built in. But certainly from C++ you would need to put in mutexes, or some other scheme.
-
@nutrx
Yes, conceptually you are allowed to access the shared data across threads.From Qt you would typically use
QMutex
/QMutexLocker
to synchronise safe access.I don't know, but it may be that Python objects across threads has this protection built in. But certainly from C++ you would need to put in mutexes, or some other scheme.
1/5