Results 1 to 2 of 2

Thread: QThread - trying to kill a qthread synchronously

  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread - trying to kill a qthread synchronously

    I've been trying to do this for a couple of days, following examples and forums tips.

    The situation is the following: I have a GUI that freezes when i call a heavy mathematical function, this way, what i want is to dispatch this heavy math function to a thread and keep the GUI responsive and so on. So far, i got this going and its ok.

    The problem starts because i need to have a 'kill button' (the user might just want to kill it since he can sometimes just change parameters and start a new one after realizing things are not going the way he wanted). I also know that this is not so recommended, the thread should never be killed/terminate - it should terminate itself - the problem here is that this heavy mathematical function isn't mine. It's legacy code and I don't have permissions to change anything inside this function.

    What i have so far:

    1) QThread and Worker


    thread = new QThread();
    worker = new Worker();
    worker->moveToThread(thread);
    connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
    connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
    connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt:irectConnection);

    2)
    ...
    ...

    worker->abort();
    thread->wait();
    worker->requestWork();

    ...
    ...

    3)

    void Worker::abort()
    {
    mutex.lock();
    if (_working) {
    _abort = true;
    qDebug()<<"Request worker aborting in Thread "<<thread()->currentThreadId();
    }
    mutex.unlock();
    }



    void Worker::requestWork()
    {
    mutex.lock();
    _working = true;
    _abort = false;
    qDebug()<<"Request worker start in Thread "<<thread()->currentThreadId();
    mutex.unlock();

    emit workRequested();
    }

    void Worker::doWork()
    {
    qDebug()<<"Starting worker process in Thread "<<thread()->currentThreadId();


    qDebug() << "get current process" << ::GetCurrentProcessId();

    threadId = thread()->currentThreadId();

    threadForTheSimulator = thread();


    mutex.lock();
    bool abort = _abort;
    mutex.unlock();


    while (abort == false)
    {
    // Checks if the process should be aborted
    mutex.lock();
    abort = _abort;
    mutex.unlock();

    // heavy mathematical function
    runCSimulator(numberOfArguments,contentOfArgs);


    }

    // Set _working to false, meaning the process can't be aborted anymore.
    mutex.lock();
    _working = false;
    mutex.unlock();

    qDebug()<<"Worker process finished in Thread "<<thread()->currentThreadId();

    emit finished();
    }

    4) the heavy computation functions is named runCSimulator(int, args)... obvisously since my function's thread doenst has a main loop or event this 'abort' function is never called.. it jumps into the function and never comes back from there.. therefore, what i think i need is a function connected with the GUI button that will cancel :

    // function triggered with the terminate button
    void Worker::doTerminate()
    {
    // here should go the code to kill the thread started
    // The problem here is that when i try to put the code
    // to terminate or quiot here, they are assynchronous.
    // I need something synchrnous that whenever the user
    // clicks the terminateButton it will shut down this thread.
    }


    Any help is really appreciated!

    Thanks a LOT!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread - trying to kill a qthread synchronously

    Quote Originally Posted by lhdamiani View Post
    The problem starts because i need to have a 'kill button' (the user might just want to kill it since he can sometimes just change parameters and start a new one after realizing things are not going the way he wanted). I also know that this is not so recommended, the thread should never be killed/terminate - it should terminate itself - the problem here is that this heavy mathematical function isn't mine. It's legacy code and I don't have permissions to change anything inside this function.
    Then you need to run that in a separate process.
    Child processes can be killed without negatively affecting the parent process.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. How to completely kill a QThread
    By Qting in forum Qt Programming
    Replies: 5
    Last Post: 12th February 2012, 19:32
  3. Need help in Qthread
    By skumar434 in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 17:21
  4. Replies: 4
    Last Post: 26th June 2008, 18:41
  5. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.