PDA

View Full Version : Simple design question with passing QVector from QThread class



alizadeh91
5th May 2013, 13:53
Hi all, hope somebody can help me,
Suppose you have a class which is called A(Is QThread) and in this class you have a big vector(QVector) of values(count =1000). All you want to do is to in every second in A you have to update values and some operations with this list also have to be done in Classes B and C.
I've confusion with design. I can send QVector via signal to those classes in every interval(class A is a simple thread). But i thought that might be slow. Isn't it better to have a pointer of vector in other classes?
Can anybody help me with this design?

ChrisW67
5th May 2013, 22:24
But i thought that might be slow.
Try it, don't guess. A modern processor will not typically take long to copy 1000 ints or doubles, but a thousand deep copies of complex structures is a different thing. You have chosen to use threads so you must either copy the data, which happens in a signal-slot call, or share a pointer and use QMutex or another synchronisation method.

alizadeh91
6th May 2013, 08:25
why i have to use QMutex?

ChrisW67
6th May 2013, 08:35
You must not allow readers of the shared data structure to read while the state of that data structure is being changed by a writer. If the writer is preempted part way through an update and the reader gets to run it might see corrupt data structures. A mutex is one way synchronise access to shared data.
http://www.wikipedia.org/wiki/Thread_%28computing%29#Concurrency_and_data_struct ures