Results 1 to 3 of 3

Thread: Help with interrupting Qthreads running infinite loops!

  1. #1
    Join Date
    Apr 2025
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Help with interrupting Qthreads running infinite loops!

    Hi! I have a similar problem to ludoQtCreator's problem where I can't get a signal-slot connection to work in a worker thread when its running a loop, but I tried implementing QCoreApplication:rocessEvents() and am still having trouble. For context, I am using a stepping motor that oscillates between 2 endpoints in an infinite while loop until asked to stop (currently I use a global variable in the main thread to stop the motion, BUT am unsure if that is the "proper" way to do that - I don't know if there are advantages to calling a function in the worker thread to do so instead). I have other threads / functions that use the exact same global var mechanism to operate, and they work - whereas this one does not. I am trying to interrupt that oscillating motion by destroying the worker thread and then creating a new worker thread. Any help would be suuuuper appreciated. Thanks!!!! I also hit the char limit so please bear with me.

    threadController inherits (is this the right term?) information from another QObject called systemHandler. systemHandler houses all the relevant data for the motors (ie motor positions). threadController handles the creation and deletion of threads and runs in the main thread. theadController creates new motorController QObjects and moves them into threads. I made motorController a child of systemHandler because I want to make a "snapshot" of the system's values whenever I direct the program to begin a motor movement - kinda like loading a cassette tape with all the pre-programmed moves into a machine.

    threadController.cpp

    Qt Code:
    1. #include "threadcontroller.h"
    2. #include "motorcontrollerclasses.h"
    3. #include "serversocket.h"
    4. #include <QObject>
    5. #include <QThread>
    6. #include <QDebug>
    7.  
    8. threadController::threadController(System *systemHandler, QObject *parent)
    9. : QObject{parent}
    10. , m_systemHandler(systemHandler)
    11. {
    12.  
    13. }
    14.  
    15. void threadController::startMotorThread() //The "spin" motor thread is an example of the infinite loop in a worker thread that is working for me.
    16. {
    17. qDebug("threadController startMotorThread() called");
    18. stopFlag = 0;
    19.  
    20. if (m_systemHandler->magnetSpinRate() > 0) {
    21. startSpinThread();
    22. emit userRequestStartSpin();
    23. }
    24.  
    25. }
    26.  
    27.  
    28. void threadController::startSpinThread()
    29. {
    30. qDebug("startSpinThread() called");
    31.  
    32. QThread *spinQThread = new QThread(); //Pointer To QThread
    33. spinQThread->setObjectName("spinThread");
    34. motorController *spinMotor = new motorController(*m_systemHandler);
    35. spinMotor->moveToThread(spinQThread);
    36. QObject::connect(spinMotor, &motorController::destroyMotorThreadSpin, spinMotor, &motorController::deleteLater);
    37. QObject::connect(spinMotor, &motorController::destroyMotorThreadSpin, spinQThread, &QThread::quit);
    38. QObject::connect(spinQThread, &QThread::finished, spinQThread, &QThread::deleteLater);
    39. QObject::connect(this, &threadController::userRequestStartSpin, spinMotor, &motorController::controlSpin);
    40.  
    41. spinQThread->start();
    42.  
    43. }
    44.  
    45. void threadController::stopMotorThread() //stopFlag shuts down ALL of my motors at once - I don't want to use every time for my adjustSweep function (in motorController).
    46. {
    47. stopFlag = 1;
    48.  
    49. qDebug("threadController stopMotorThread() called");
    50.  
    51. }
    52.  
    53.  
    54. void threadController::startAdjustSweepMotorThread() //the "adjustSweep" is the one I am having trouble with interrupting and destroying the thread.
    55. {
    56. stopAdjustSweepFlag = 0; //stopAdjustSweepFlag is my global var. A value of 1 is supposed to interrupt the sweep. I was noodling around and am using 2 methods to try and stop it - global var and calling a method.
    57. startAdjustSweepThread();
    58. emit startAdjustSweepSignal();
    59.  
    60. }
    61.  
    62. void threadController::startAdjustSweepThread()
    63. {
    64. qDebug("startAdjustSweepThread called");
    65.  
    66. QThread *adjustSweepQThread = new QThread(); //Pointer To QThread
    67. adjustSweepQThread->setObjectName("adjustSweepThread");
    68. motorController *adjustSweepMotor = new motorController(*m_systemHandler); //(m_systemHandler, this);
    69. adjustSweepMotor->moveToThread(adjustSweepQThread);
    70. QObject::connect(this, &threadController::threadStopAdjustSweepSignal, adjustSweepMotor, &motorController::stopAdjustSweep, Qt::QueuedConnection); //This signal is successfully emitted, but the slot is never called!
    71. QObject::connect(this, &threadController::threadStopAdjustSweepSignal, adjustSweepMotor, &motorController::test, Qt::QueuedConnection); //Similar story to the above!
    72. QObject::connect(adjustSweepMotor, &motorController::destroyMotorThreadAdjustSweep, adjustSweepMotor, &motorController::deleteLater);
    73. QObject::connect(adjustSweepMotor, &motorController::destroyMotorThreadAdjustSweep, adjustSweepQThread, &QThread::quit);
    74. QObject::connect(adjustSweepQThread, &QThread::finished, adjustSweepQThread, &QThread::deleteLater);
    75. QObject::connect(this, &threadController::startAdjustSweepSignal, adjustSweepMotor, &motorController::adjustSweep, Qt::QueuedConnection);
    76.  
    77. //Below is what chatGPT told me to try, I get the signal received statement, but the invokeMethod() doesn't work and can't find the function.
    78. // QObject::connect(this, &threadController::threadStopAdjustSweepSignal, []() {
    79. // qDebug() << "Signal threadStopAdjustSweepSignal received";
    80. // });
    81. //QMetaObject::invokeMethod(adjustSweepMotor, "stopAdjustSweep", Qt::QueuedConnection);
    82.  
    83. adjustSweepQThread->start();
    84. }
    To copy to clipboard, switch view to plain text mode 

    threadController.h

    Qt Code:
    1. #ifndef THREADCONTROLLER_H
    2. #define THREADCONTROLLER_H
    3. #include <QObject>
    4. #include "system.h"
    5.  
    6. class threadController : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit threadController(System *systemHandler, QObject *parent = nullptr);
    11. //explicit threadController(QObject *parent = nullptr);
    12.  
    13. public slots:
    14. void startMotorThread();
    15. void stopMotorThread();
    16. void stopRotateMotorThread();
    17.  
    18. void startRotateMotorThreadPrecise();
    19. void startAdjustMotorThread();
    20. void startAdjustSweepMotorThread();
    21.  
    22. void startSpinThread();
    23. void startRollThread();
    24. void startAdjustThread();
    25. void startAdjustSweepThread();
    26. void startRotateThreadPrecise();
    27. void startInitializeThread();
    28. void startStowThread();
    29. void startStowRotateThread();
    30. void startInitializeRotateThread();
    31. void startInitializeRollThread();
    32.  
    33. void rollSignalNotifier();
    34. void rotateSignalNotifier();
    35. void rotateStowSignalNotifier();
    36.  
    37. void stopAdjustRollCalled();
    38. void stopAdjustSweepCalled();
    39.  
    40. signals:
    41. //void userRequestStart();
    42. void userRequestStartSpin();
    43. void userRequestStartRoll();
    44. void userRequestStartRotatePrecise();
    45. void userRequestStartInitializeRotate();
    46. void userRequestStartInitializeRoll();
    47. void userRequestStartStowRotate();
    48. void startAdjustRollSignal();
    49. void startAdjustSweepSignal();
    50. void threadStopAdjustSweepSignal();
    51.  
    52.  
    53. private:
    54. System *m_systemHandler;
    55.  
    56. };
    57.  
    58. extern int stopFlag; //This is the stopFlag to initiate stopping of the motors.
    59. extern int stopRotate; //StopFlag to stop rotate motors.
    60. extern int stopSpinFlag;
    61. extern int stopRollFlag;
    62. extern int stopAdjustRollFlag; //stopFlag to end the adjust movement
    63. extern int stopAdjustSweepFlag; //stopFlag to end sweep movement.
    64.  
    65.  
    66. #endif // THREADCONTROLLER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,311
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with interrupting Qthreads running infinite loops!

    Without too much study of your code, one thing I notice is that each time you create a new "motorController" instance you are passing "*m_systemHandler". This is creating a copy of the System instance pointed to by m_systemHandler, meaning each motorController instance has its own unique System instance, not a pointer to what I think you want as the main System instance passed to the threadController constructor.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2025
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with interrupting Qthreads running infinite loops!

    Hi sorry! I actually figured it out! I just needed to add Q_INVOKABLE in front of the function that wasn't working. I also realized I should have boiled the code down into something a bit more understandable haha.

Similar Threads

  1. Pausing a Thread running an infinite loop (Qt+OpenCV)
    By franco.amato in forum Qt Programming
    Replies: 4
    Last Post: 29th October 2019, 23:10
  2. Interrupting QEventLoop with signals ?
    By oldFox64 in forum Newbie
    Replies: 2
    Last Post: 4th April 2014, 11:21
  3. Interrupting script evaluation of QScriptEngine
    By Haqim in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2010, 14:38
  4. for loops in c++
    By baray98 in forum General Programming
    Replies: 16
    Last Post: 19th January 2009, 14:09
  5. QApplication and widgets' loops
    By Placido Currò in forum Qt Programming
    Replies: 7
    Last Post: 31st January 2008, 12:13

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.