Results 1 to 4 of 4

Thread: question about threads: calling 'start()->"

  1. #1
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default question about threads: calling 'start()->"

    Hi,

    i was wondering something about threads :

    if you initalize your class, and then you call its instance with "start()", won't this class be called two times, once for the instance, and the second time for the thread pool? (whereas we just want to call the class once, on a separate thread)

    example :

    Qt Code:
    1. void MyClient::readyRead(){
    2. MyTask *mytask = new MyTask();
    3. …
    4. QThreadPool::globalInstance()->start(mytask);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    253
    Thanks
    23
    Thanked 16 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: question about threads: calling 'start()->"

    Hi,

    Why you just not use QThread. For example:

    Qt Code:
    1. class myBigProcess : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit myBigProcess(QObject *parent = 0);
    6.  
    7. void run();
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. myBigProcess::myBigProcess(QObject *parent) :
    2. QThread(parent)
    3. {
    4.  
    5. }
    6. void myBigProcess::run()
    7. {
    8. ///Massive process here
    9. }
    To copy to clipboard, switch view to plain text mode 

    Then you can do:
    Qt Code:
    1. myBigProcess *MBP;
    2.  
    3. MBP = new myBigProcess(this);
    4. MBP->start(); //This will call run();
    To copy to clipboard, switch view to plain text mode 

    QThread has signals to know when the run() finished.

    Carlos.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about threads: calling 'start()->"

    QThreadPool::start starts a run method on the passed object and initializes it does not.

  4. #4
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: question about threads: calling 'start()->"

    oh okay, thanks for your answers

Similar Threads

  1. Missing "+" when calling QNetworkAccessManager.post()?
    By MorrisLiang in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2010, 05:15
  2. Does "emit" in the calling thread mean execution?
    By nomadoro in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2009, 12:16
  3. "emit" keyword optional when calling signals?
    By will49 in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 02:13
  4. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09:55
  5. How to sync threads at start-up
    By Artschi in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2006, 12:18

Tags for this Thread

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.