Results 1 to 2 of 2

Thread: Threading an instance of a class which has no dependency upon Qt

  1. #1
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Threading an instance of a class which has no dependency upon Qt

    I need to be able to launch a sub-classed QThread in such a way that the run() protected method of QThread calls different objects depending upon a class-member variable. Let's see an example.

    This is the header of my sub-classed QThread:

    Qt Code:
    1. #ifndef THREADLAUNCHER_H
    2. #define THREADLAUNCHER_H
    3.  
    4. #include <QThread>
    5. #include "someProcess.h"
    6.  
    7. class threadLauncher : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. void run() ;
    12. void setClass(someProcess * aProcess) ;
    13. private:
    14. someProcess * theProccess_ ;
    15. };
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 

    Now the source-code of the implementation:

    Qt Code:
    1. #include "someProcess.h"
    2. #include "threadLauncher.h"
    3.  
    4. void threadLauncher::run()
    5. {
    6. theProccess_->compute() ;
    7. }
    8.  
    9. void threadLauncher::setClass(someProcess * aProcess)
    10. {
    11. theProccess_ = aProcess ;
    12. }
    To copy to clipboard, switch view to plain text mode 

    In the main program this is the way I launch the thread:

    Qt Code:
    1. int main()
    2. {
    3. QApllication app() ;
    4.  
    5. someProcess * theProcessA_ = new someProcess() ;
    6.  
    7. threadLauncher * theThreadLauncher_ = new threadLauncher() ;
    8. theThreadLauncher_->setClass(theProcessA_) ;
    9. theThreadLauncher_->start() ;
    10.  
    11. app.exec() ;
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is working fine: now comes the tricky part (and my question).
    I would like to execute different things in the re-implemented 'run' method. One (trivial) possibility could be:

    Qt Code:
    1. int main()
    2. {
    3. QApllication app() ;
    4.  
    5. someProcess * theProcessA_ = new someProcess() ;
    6. classBDefinition * theProcessB_ = new classBDefinition() ;
    7.  
    8. threadLauncher * theThreadLauncher_ = new threadLauncher() ;
    9. theThreadLauncher_->setClass(theProcessA_) ;
    10. theThreadLauncher_->start() ;
    11.  
    12. theThreadLauncher_->setClass(theProcessB_) ;
    13. theThreadLauncher_->start() ;
    14.  
    15. app.exec() ;
    16. }
    To copy to clipboard, switch view to plain text mode 

    This would allow me to launch two completely different computations (implemented in different classes) using a single instance of a threadLauncher. My problem at this point is how to make the mechanism general (template?). Infact, to be able to run the latter example I should modify the threadLauncher to become the following:

    Qt Code:
    1. #ifndef THREADLAUNCHER_H
    2. #define THREADLAUNCHER_H
    3.  
    4. #include <QThread>
    5. #include "someProcess.h"
    6. #include "classBDefinition.h"
    7.  
    8. class threadLauncher : public QThread
    9. {
    10. Q_OBJECT
    11. public:
    12. void run() ;
    13. void setClass(someProcess * aProcess) ;
    14. void setClass(classBDefinition * aProcessB) ;
    15. private:
    16. someProcess * theProccessA_ ;
    17. classBDefinition * theProccessB_ ;
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    and the source, accordingly:

    Qt Code:
    1. #include "someProcess.h"
    2. #include "threadLauncher.h"
    3.  
    4. void threadLauncher::run()
    5. {
    6. if( theProccessA_ ) theProccessA_->compute() ;
    7. if( theProccessB_ ) theProccessB_->compute() ;
    8. }
    9.  
    10. void threadLauncher::setClass(someProcess * aProcess)
    11. {
    12. theProccessA_ = aProcess ;
    13. }
    14.  
    15. void threadLauncher::setClass(classBDefinition * aProcess)
    16. {
    17. theProccessB_ = aProcess ;
    18. }
    To copy to clipboard, switch view to plain text mode 

    This is obviously not manageable should the possibilities increase. My first though was to make the threadLauncher a template class, but since it inherits from a QThread I cannot (I was thinking something like this would do:

    Qt Code:
    1. #ifndef THREADLAUNCHER_H
    2. #define THREADLAUNCHER_H
    3.  
    4. #include <QThread>
    5.  
    6. template <class T> threadLauncher : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. void run() ;
    11. void setClass(T * aProcess) ;
    12. private:
    13. T * theProccessA_ ;
    14. };
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    I know this is absolutely incorrect C++, I meant it just to convey the basic idea.

    Is there some useful suggestion I could receive?

    Thanks
    Last edited by menasce; 17th December 2010 at 16:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Threading an instance of a class which has no dependency upon Qt

    Before getting in to details - have you checked QtConcurrent?
    It looks to me, your case exactly meant for that.
    It will save you the whole sub classing issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Can you use dependency injection with Qt?
    By photo_tom in forum Qt Programming
    Replies: 0
    Last Post: 20th February 2010, 18:34
  2. Library dependency in Qt 4.6
    By Tino in forum Installation and Deployment
    Replies: 1
    Last Post: 7th December 2009, 18:28
  3. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 9th August 2009, 23:51
  4. Replies: 3
    Last Post: 7th May 2008, 11:33
  5. DLL dependency?
    By duschl in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2006, 17:34

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.