Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread::terminate doesn't work

QThread::terminate doesn't work

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.3k Views
  • 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.
  • D Offline
    D Offline
    dlodyakov
    wrote on last edited by
    #1

    Hi. I'm trying to terminate thread, I know that it's wrong, and I have another way to terminate thread, but sometimes normal way will not work but I still need to terminate thread. So I try to use QThread::terminate and this is didn't work. I try to use QThread::terminationEnabled(true / false) but this is doesn't help me. And also I'm running it on Linux. Can you help me to solve that problem?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Is your thread not terminated or is not terminated immediately?

      [quote]Terminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating systems scheduling policies. Use QThread::wait() after terminate() for synchronous termination.[/quote]

      One should mention that - as you obviously already know - you should not terminate threads - especially if it is just a workaround for broken code.

      You might post some code so we can find the actual error.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dlodyakov
        wrote on last edited by
        #3

        I can't terminate thread in all case. This is my code (not exactly my code, but idea the same)

        @void Foo::loop()
        {
        while(true)
        {
        if(_terminated)
        {
        return;
        }
        runSomeFunctionWhichICantTerminate();
        }
        }

        void Foo::terminate()
        {
        _terminated = true;
        }
        @
        Than in thread I run this function.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dlodyakov
          wrote on last edited by
          #4

          @class MyThread: public QThread
          {
          public:
          void setFoo(Foo *foo) { _foo = foo; }
          void run() { _foo->loop(); }
          private:
          Foo *_foo;
          };

          class Bar
          {
          public:
          void doSomething()
          {
          _thread->setFoo();
          _thread->start();
          }
          slots:
          void terminate()
          {
          // If we can't terminate with normal way - will use hard termination, timeout will exeucte hardTerminate
          timer.singleShot(1000, this, SLOT(hardTerminate()));

          // Normal termination
          _foo->terminate();
          
          // Waiting thread
          _thread->wait();
          
          // Thread finished, cancel timer
          timer.stop();
          

          }
          void hardTerminate()
          {
          // Ooops, normal termination failed, will terminate thread
          _thread->terminate();
          }
          private:
          Foo *_foo;
          MyThread *_thread;
          QTimer _timer;
          @

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved