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. Remove scheduled thread from QThreadPool
Qt 6.11 is out! See what's new in the release blog

Remove scheduled thread from QThreadPool

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

    Hi,

    is it possible to remove a thread which is scheduled through QConcurrent::run for running from the schedule i. e. something happened in one of the threads currently running and now I want to stop all other threads because I don't need their results any more.

    Thanks,
    butterface

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Why not simply define an abort flag and give each thread a pointer to that flag in the constructor? The thread would then check the flag in its run() method before it starts work (and maybe also periodically). If the flag has been set for whatever reason, the thread will abort ASAP. At least that's what I do ;-)

      @volatile bool globalAbortFlag = false;

      void startJobs(void)
      {
      globalAbortFlag = false;
      while(moreJobsAvailable)
      {
      m_threadPool.start(new MyTask(&globalAbortFlag));
      }
      )

      void abortJobs(void)
      {
      globalAbortFlag = true;
      m_threadPool.waitForDone();
      }

      //----------

      class MyTask : public QRunnable
      {
      public:
      MyTask(volatile bool *abortFlag)
      {
      m_abortFlag = abortFlag;
      }

      virtual void run(void)
      {
          while(!jobCompleted)
          {
              if(*m_aborted) return;
              perfromNextStep();
          }
      }
      

      private:
      volatile bool *m_aborted;
      }
      @

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on last edited by
        #3

        Okay that sounds appropriate. But there is no other way to remove them?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          By looking at the QThreadPool documentation:
          No

          And it's also clear why: How should the ThreadPool be able to abort a job once it is running? If that job has no special function/flag to abort the job - and QRunnable has no such such API - only the entire thread could be terminated. Killing a thread always comes at the danger of leaving resources in an undefined state (resource leak an other evil things).

          Well, at least jobs not started yet could be discarded (descheduled), using some removeAllPendingJobs() function. But maybe these jobs should never have be scheduled instead of removing them again. In general you have to assume that a job might be started immediately, once it has been added.

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          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