Results 1 to 11 of 11

Thread: Worker thread doesn't seem to start event loop?

  1. #1
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Question Worker thread doesn't seem to start event loop?

    Hi everyone,
    I'm using QTimers in a multithread Qt5 application. Here is my structure:
    - MainWindow.cpp creates a thread A and its worker object:
    Qt Code:
    1. t = new QThread();
    2. worker = new SudokuComputeWorker();
    3. worker->moveToThread(t);
    4. connect(t, SIGNAL(started()), worker, SLOT(doWork()));
    5. connect(worker, SIGNAL(scoresFetched(const QList<double>*)), worker, SLOT(workDone()));
    6. t->start();
    To copy to clipboard, switch view to plain text mode 

    - Thread A creates N threads B1, B2,... BN, each with its own worker object:
    Qt Code:
    1. foreach (SearchComponent* s, algos) {
    2. QThread* t = new QThread();
    3. SudokuWorker* w;
    4. w = new SudokuWorker(i, s, _myBLOCKS_SIZE, _myON_DISTRIBUTION, _myHOW_MANY_PROBLEMS);
    5. w->moveToThread(t);
    6. connect(t, SIGNAL(started()), w, SLOT(doWork()));
    7. connect(w, SIGNAL(scoreFetched(int)), w, SLOT(workDone()));
    8. t->start();
    9. }
    To copy to clipboard, switch view to plain text mode 

    - Whats happens in SudokuWorker::doWork()?
    Qt Code:
    1. void SudokuWorker::doWork()
    2. {
    3. QTimer* t = new QTimer(NULL);
    4. my_AlgoToTest->attachTimer(t);
    5.  
    6. for (; _myNbTimesToRun > 0 && !_myInterruptNow; _myNbTimesToRun--) {
    7. qDebug() << *my_AlgoToTest << _myNbTimesToRun;
    8. SudokuState s(my_BlockSize);
    9. my_AlgoToTest->resetAlgorithm();
    10. my_AlgoToTest->runAlgorithm(&s);
    11. scores.append(my_AlgoToTest->getBestReward());
    12. testsCompleted++;
    13. }
    14.  
    15. delete t;
    16. emit scoreFetched(my_AlgoIndex);
    17. }
    To copy to clipboard, switch view to plain text mode 
    [Notice: Normal if the sense of the code doesn't seem obvious, I just let the things in relation with the threads and the worker. Threads and objects deletion is properly set.]

    Note that in attachTimer(), the timer is set as "single shot: true". Moreover, in runAlgorithm(), there is the "timer->start(1000)" statement.

    Now the problem:
    The timers aren't started, as if the threads in which the workers are had no event loops. Indeed, when I want to show the remaining time of the timers juste after they have started, I have random numbers (all but 1000) that shows up. This is the mark of an uninitialized value, and it occurs because somehow the timers don't find their event loop.
    Have you any idea of what I'm doing wrong? I thought I applied as I had to the method presented in "You're doing it wrong"...

    Thank you!!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Worker thread doesn't seem to start event loop?

    Thread A creates N threads B1, B2,... BN, each with its own worker object:
    Whre is code in the actual software? I mean which method of Thread A?


    Qt Code:
    1. void SudokuWorker::doWork()
    2. {
    3. QTimer* t = new QTimer(NULL); // Timer is createed here
    4. my_AlgoToTest->attachTimer(t);
    5.  
    6. // Note: Timers/events will not be triggered while inside this for loop, uness explicit event processing is done.
    7. for (; _myNbTimesToRun > 0 && !_myInterruptNow; _myNbTimesToRun--) {
    8. qDebug() << *my_AlgoToTest << _myNbTimesToRun;
    9. SudokuState s(my_BlockSize);
    10. my_AlgoToTest->resetAlgorithm();
    11. my_AlgoToTest->runAlgorithm(&s);
    12. scores.append(my_AlgoToTest->getBestReward());
    13. testsCompleted++;
    14. }
    15.  
    16. delete t; // Timer is deleted here. How can you expect the deleted timer to work?
    17.  
    18. emit scoreFetched(my_AlgoIndex);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Worker thread doesn't seem to start event loop?
    QThread by defaut starts it's event loop when start() is called, provided run() is not reimplemented. The effect of event loop can only be seen once the code control returns to the event loop. Just looping in a for loop / while loop will not trigger any events/inter thread signals/etc. All events will be handled only when the control returns to the event loop.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Worker thread doesn't seem to start event loop?

    Hi and thanks for helping me !!
    The thing is that the "runAlgorothm" is the really long part. This loop is not the thread creation loop but the algorithm test loop. So the timer is deleted after all tests have been done (for each test, I restart the timer in runAlgorithm).

    The second piece of code I showed you is the doWork method of thread A, that is in charge to create the threads Bi.

    I don't exactly understand when you say that the event loop is stucked as long as I'm looping in my algorithms tests. The run() method of threads A and Bi are left untouched, not reimplemented. I just connect the started() signal of the thread to the doWork() method of its worker object... So technically, when calling start(), the thread is created, the original run method is run, that is only exec(), then "started()" signal is fired and doWork is called... No ?
    There is no code in run so I don't understand how the event loop could be stucked? I think I haven't understood something... And why are my timers not started properly, as if the event loop wasn't already started?

    Thank you again!!

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Worker thread doesn't seem to start event loop?

    So technically, when calling start(), the thread is created, the original run method is run, that is only exec(), then "started()" signal is fired and doWork is called... No ?
    You are correct.

    The point you are missing is that the timer events/signals are called/emitted from event loop, and that will occur only when control returns to the event loop. Here the doWork() (or whatever slot) is buzy doing something and thus control does not return to the event loop. Note all the slots of an objects in a thread and the event loop are executed in the same context/control/stack, which means when a slot of an object in a thread is executing another slot/event/signal of an object of same thread has to wait for the first object's slot to finish and wait for the control to return to event loop.
    Last edited by Santosh Reddy; 31st August 2013 at 20:10.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Worker thread doesn't seem to start event loop?

    oh Ok, now I undesrstand : it is stucked because it waits for the slot to terminate (which will never terminate as it waits for another signal that will never be processed).
    Do you have any idea of how to achieve this using the correct use of the worker threads?
    Thank you for your enlightenings so far!!

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Worker thread doesn't seem to start event loop?

    I don't see a reason to use QTimers, just use multiple threads with out any timers.

  7. #7
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Worker thread doesn't seem to start event loop?

    The thing is that I need them because I test my algorithms performances for some research and they all have a computational budget of X seconds. So I need QTimers for each to use that precise amount of time and not more...

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Worker thread doesn't seem to start event loop?

    The thing is that I need them because I test my algorithms performances for some research and they all have a computational budget of X seconds. So I need QTimers for each to use that precise amount of time and not more...
    Looks like you want some time based scheduling which makes sure that algorithm doe not run for more than X seconds. or may you just want to measure the time consumed by each algorithm. In either cases QTimer will not help, you have use QTime
    Qt Code:
    1. t.start();
    2. some_lengthy_task();
    3. qDebug("Time elapsed: %d ms", t.elapsed());
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Worker thread doesn't seem to start event loop?

    That's perfect ! I didn't know about QTime, this should do the trick !
    Thanks again for everything

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Worker thread doesn't seem to start event loop?

    Or even QElapsedTimer if you don't need to convert into hours/minutes/seconds and just want the most efficient class http://qt-project.org/doc/qt-5.0/qtc...psedtimer.html

    Cheers,
    _

  11. #11
    Join Date
    Aug 2013
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Worker thread doesn't seem to start event loop?

    Indeed, it's even better for my use! Thank you

Similar Threads

  1. Replies: 5
    Last Post: 22nd June 2012, 16:40
  2. Replies: 5
    Last Post: 8th February 2012, 09:56
  3. Event loop does not start
    By koan in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 31st August 2010, 14:26
  4. Replies: 10
    Last Post: 15th January 2010, 14:35
  5. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10

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.