Return to site

Qthread Slot Quit

broken image


I wrote about multi-threading in Qt a while back, and how to use QThread to wrap a blocking function call 'lock free'. Today we'll talk about how to pass data into your thread. This approach can be used, for example, to tell your QThread to stop.

  1. Qthread Slot Quitman
  2. Qthread Requestinterruption
  3. Qthread Lock

1 Worker finished signal will call quit on the thread. This will end the thread's event loop and initiates the thread finished signal. 2 Worker finished signal is connected to the worker deleteLater. According to deleteLater documentation. You can see my code below: bool captureHandler::newConnection (cv::VideoCapture &capture, QString address) QThread. thread = new QThread; streamConnector. c = new streamConnector (capture, address); c-moveToThread (thread); connect (thread, SIGNAL (started ), c, SLOT (process )); connect (c, SIGNAL (finished ), thread, SLOT (quit )); //Does not work? Connect (c, SIGNAL (finished ), c, SLOT (deleteLater )); connect (thread, SIGNAL (finished ), thread, SLOT (deleteLater. Andrei has 15+ years working for the likes of Microsoft, EMC, Motorola, and Deutsche Bank on mobile, desktop, and web using C, C#, and JS. C developers strive to build robust multithreaded Qt applications, but multithreading was never easy with all those race conditions, synchronization,. Qt/C - Lesson 048. QThread — How to work with threads using moveToThread. In a previous article we are only a little touch to working with threads, and in the version that is more for the customization of thread, although it can be used to perform o.

Qthread Slot Quit

There are two ways to use QThread, with and without an event loop, and the preferred method for talking to a QThread depends on which of them you use.

Qthread Slot Quitman

Way 1: Without an event loop

Slots

When you're not using an event loop, the thread's run() method often looks like this:

Qthread

There are two ways to use QThread, with and without an event loop, and the preferred method for talking to a QThread depends on which of them you use.

Qthread Slot Quitman

Way 1: Without an event loop

When you're not using an event loop, the thread's run() method often looks like this:

Since this method does not use an event loop, there is no way to deliver a signal to your thread. So if you want to pass data into the thread, you have to use a good old fashioned mutex. Let's look at an example showing you how to stop your thread:

Harrahs casino from my location. In this case, we have to check the stopRequested variable in a timely manner in our thread's run() method. The longer you run between checks, the longer it will take your thread to actually stop.

Outside observers can use the finished() signal to know when your thread is actually done. So if you are in a QMainWindow, for example, and a closeEvent() happens, you can ignore the event, call MyThread::stop(), and then when the QThread::finished() signal arrives, you can actually close the window.

The downside is that the stop() call will actually block while it tries to acquire the mutex. Given the way this code is written, the blocking will probably be very short, but hey, I hate blocking. Let's see if we can dig up a better way to do this.

Qthread Requestinterruption

Way 2: With an event loop

If you have an event loop, you can use Qt's meta-objects to talk to your thread. Let's look at the same example as before, only this time with no locking or blocking.

Look mom! No locks! Now we have killed our thread, safely and gracefully. There is no chance of blocking, and we learned something about QMetaObject.

A couple items to note:

  • The doSomething() method is left as an exercise for the reader, but be careful about the QTimer interval. I used 0, which means it will be firing almost constantly.
  • The stop() method must be a slot in MyThread (and not just a regular method) or else invokeMethod() will return false and not actually re-invoke stop() for you.
  • You can pass arguments to your thread this way, but it requires a bit more fun with QMetaObject::invokeMethod().
  • You can reduce this whole thing to a magical macro that you could put at the top of your stop() method, saving you from having to write if(currentThread() this) at the top of every method. Hint: use the __FUNCTION__ macro.
  • To run this example code, you'll need to #include these files: QThread, QTimer, QMetaObject, QMutexLocker, and QMutex
  • To call quit(), it may not actually be necessary to be running on the same QThread (it works for me without the QMetaObject), but this will be required when you start passing in data to your thread. Without it, your program could do unpredictable naughty things. I can't find anything in the docs about whether quit() is thread safe.

I've found this QMetaObject approach the most effective and easiest way to pass data to QThreads safely, without blocking and without locks.

Qthread Lock

Happy threading! Trump taj mahal casino atlantic city nj.





broken image