Results 1 to 4 of 4

Thread: Simple design question with passing QVector from QThread class

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Simple design question with passing QVector from QThread class

    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?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Simple design question with passing QVector from QThread class

    Quote Originally Posted by alizadeh91 View Post
    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.

  3. #3
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple design question with passing QVector from QThread class

    why i have to use QMutex?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Simple design question with passing QVector from QThread class

    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...ata_structures

  5. The following user says thank you to ChrisW67 for this useful post:

    alizadeh91 (6th May 2013)

Similar Threads

  1. Replies: 2
    Last Post: 17th March 2011, 14:30
  2. Simple QVector sort?
    By bizmopeen in forum Newbie
    Replies: 3
    Last Post: 16th February 2010, 18:50
  3. Replies: 2
    Last Post: 1st May 2009, 08:35
  4. Replies: 2
    Last Post: 27th March 2007, 13:09
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 23:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.