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. Kill QThreadPool
Forum Updated to NodeBB v4.3 + New Features

Kill QThreadPool

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 8.5k 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.
  • G Offline
    G Offline
    giuspo77
    wrote on last edited by
    #1

    Hello Users,
    I’m new in this forum and I have few questions using QtConcurrent :: run.
    Basically I'm trying to test how a program can be robust, using more thread with "QtConcurrent::run", to write to a database in a non-blocking way.
    It works perfectly in my procedure using "QtConcurrent::run" and connecting to a SQL Express Database with ODBC. To test I put a "Sleep" into my procedure (to simulate a "Long-Run" query) but when I close the program all my objects are destroyed except the thread created by "QtConcurrent::run". So the program then gets stuck and does not end immediately because of "Sleep". I discovered, in QT Help, that "Qtconcurrent" makes use of "QThreadPool", but I have not found anything that can stop instantly this thread. Do you have any suggestion about it?

    Thanks a lot
    Giulio.

    1 Reply Last reply
    0
    • joborJ Offline
      joborJ Offline
      jobor
      wrote on last edited by
      #2

      You should cancel the long operation in the thread in a safe way instead of trying to kill the thread.
      You can wait for the termination of the threads in the thread pool with QThreadPool::waitForDone().

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

        You could create some global "abort" flag and pass a pointer to that flag into each function you call via QtConcurrent::run(). Inside the function you check the value of the flag (by using the pointer) and, if the flag has been set, return as soon as possible. When your application is about to exit, just set the flag...

        @int someFunction(/other arguments/, bool *abortFlag)
        {
        while(workLeft && (*abortFlag != true))
        {
        /do more work/
        }
        }@
        @bool abortFlag = false;
        extern QString someFunction(/other arguments/, bool *abortFlag);
        QByteArray bytearray = ...;

        QFuture<QString> future = QtConcurrent::run(someFunction, bytearray, &abortFlag);@
        @abortFlag = true;
        QThreadPool::globalInstance()->waitForDone();@

        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
        • G Offline
          G Offline
          giuspo77
          wrote on last edited by
          #4

          Ok. Thanks for your suggestions.

          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