Results 1 to 3 of 3

Thread: proper method for dynamically creating QThreads

  1. #1
    Join Date
    May 2011
    Posts
    1
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default proper method for dynamically creating QThreads

    I have a GUI which displays data traces using opengl. To make everything work nice, I've moved the code which updates the data to another thread. So I have something like this:

    Qt Code:
    1. class DataObjectGL : QWidgetGL {
    2. public:
    3. DataUpdate : QObject
    4. }
    5.  
    6. DataObjectGL :: DataObjectGL() {
    7.  
    8. dataUpdate = new DataUpdate();
    9. dataUpdate->moveToThread(&GUIDataProcessThread);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    I would like to generate an arbitrary (small) number of these objects depending on the GUI layout.

    I have found two ways of signalling the existence of new data. If I use a signal connected to a slot in dataUpdate, everything works great. However, for efficiency's sake, I'd like to use a QWaitCondition. However, that means that the data processing code must run in an infinite loop in dataUpdate. And when I start this loop (by connecting to the thread start() signal), the thread only gets to start the first object's loop. Am I misunderstanding QWaitCondition? Can multiple QWaitConditions not run in a single thread?

    thanks!
    Last edited by high_flyer; 18th May 2011 at 09:22. Reason: code tags

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: proper method for dynamically creating QThreads

    How is new data detected? If it is something you check after a fixed time you could run a QTimer and do the check then the processing on the timeout signal to avoid having threads waiting.

    For spawning a bunch of processing threads I would recommend you check the documentation for QRunnable and QThreadPool, it will take care of managing the threads is an efficient way.

  3. The following user says thank you to Berryblue031 for this useful post:

    kemere (24th May 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: proper method for dynamically creating QThreads

    Wait conditions are not bound to a thread. They just provide a synchronisation point where the behaviour of threads is controlled using a mutex. You can have as many wait conditions per thread as you want but only one of them can restart the thread (the one a particular thread waits for). For what you want to do I'd probably recommend using QtConcurrent although it depends on what your data feeding thread will actually be doing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    kemere (24th May 2011)

Similar Threads

  1. Dynamically creating buttons from Xml file
    By chandru@080 in forum Newbie
    Replies: 5
    Last Post: 25th November 2010, 10:34
  2. Replies: 3
    Last Post: 15th April 2010, 13:26
  3. Creating dock widget as tab dynamically
    By vidyadhar in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2009, 12:03
  4. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  5. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05

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
  •  
Qt is a trademark of The Qt Company.