Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Restarting a QThread

    General and Desktop
    3
    3
    2705
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      Nubcake last edited by

      I have a question about QThread, start() and quit().

      I have a QThread and a worker object that is run in the QThread via moveToThread(). At some point, the worker object calls a function send() that starts sending data, however at some point I call quit() then wait for it to finish via wait(). What I noticed is that when I start the QThread object again through start(), execution of the thread resumes at the last point where it was stopped. Is this normal behavior?

      How can I reset the thread to the state it was at creation along with the worker object?

      Is the only way to do this by stopping the thread normally through quit(), delete the worker object, and create a new worker object, calling moveToThread() on the same thread with the new object before finally calling start? I've tried this way but I get random read access violation errors sometimes; the debugger always jumps to the same line inside the file called xthread:

      inline int _Cnd_broadcastX(_Cnd_t *_Cnd)
      {     // throw exception on failure
        return (_Check_C_return(_Cnd_broadcast(_Cnd)));
      }
      
      f:\dd\vctools\crt\crtw32\concrt\event.cpp:693: error: Expcetion at 0x237eb5c, code 0xc0000005 read access violation at: 0x0, flags=0x0 (first chance)
      

      Googling this line of code didn't yield any results. Is this a thread related error?

      Advice appreciated.
      Thanks

      JKSH 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Can you show how you implement your worker object and thread handling ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • JKSH
          JKSH Moderators @Nubcake last edited by JKSH

          @Nubcake said in Restarting a QThread:

          How can I reset the thread to the state it was at creation along with the worker object?

          Well, QThread::quit() alone doesn't do anything to your worker object, so it remembers its last state.

          Implement an initialization function in your worker object, and make sure this function is called (in the correct thread!) when you start() the thread.

          EDIT 1: To clarify, quit() stops the thread's event loop. That's all.
          EDIT 2: Actually, why do you need to quit the thread? Could you just leave the thread running permanently, and implement a state machine in your worker?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply Reply Quote 4
          • First post
            Last post