Qt Thread Safe Signal Slot

Posted : admin On 4/12/2022

Connecting in Qt 5. There are several ways to connect a signal in Qt 5. Qt 5 continues to support the old string-based syntax for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget). Qt provides the signals and slots framework which allows you to do just that and is thread-safe, allowing safe communication directly from running threads to your GUI frontend. Signals allow you to.emit values, which are then picked up elsewhere in your code by slot functions which have been linked with.connect.

  1. Qt Thread Safe Signal Slot Adapter
  2. Qt Signal Slot Example

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.

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.

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:

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.

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.

Happy threading!

Home · All Classes · Modules

The QThread class provides a platform-independent way to managethreads. More...

Inherits QObject.

Types

  • enum Priority { IdlePriority, LowestPriority, LowPriority, NormalPriority, ..., InheritPriority }

Methods

  • int exec_ (self)
  • bool isFinished (self)
  • Priority priority (self)
  • run (self)
  • setStackSize (self, int stackSize)
  • start (self, Priority priority = QThread.InheritPriority)
  • bool wait (self, int msecs = ULONG_MAX)

Static Methods

  • int currentThreadId ()
  • msleep (int)
  • sleep (int)
  • yieldCurrentThread ()

Qt Signals

  • void started ()

Detailed Description

The QThread class provides a platform-independent way to managethreads.

A QThread object manages one thread of control within theprogram. QThreads begin executing in run(). By default, run() starts the event loop by callingexec_() and runs a Qt event loopinside the thread.

You can use worker objects by moving them to the thread usingQObject.moveToThread().

The code inside the Worker's slot would then execute in aseparate thread. However, you are free to connect the Worker'sslots to any signal, from any object, in any thread. It is safe toconnect signals and slots across different threads, thanks to amechanism called queuedconnections.

Another way to make code run in a separate thread, is tosubclass QThread and reimplement run(). For example:

In that example, the thread will exit after the run function hasreturned. There will not be any event loop running in the threadunless you call exec_().

It is important to remember that a QThread instance lives in the old thread thatinstantiated it, not in the new thread that calls run(). This means that all of QThread'squeued slots will execute in the old thread. Thus, a developer whowishes to invoke slots in the new thread must use the worker-objectapproach; new slots should not be implemented directly into asubclassed QThread.

When subclassing QThread, keep in mind that the constructorexecutes in the old thread while run() executes in the new thread. If amember variable is accessed from both functions, then the variableis accessed from two different threads. Check that it is safe to doso.

Note: Care must be taken when interacting with objectsacross different threads. See Synchronizing Threads fordetails.

Managing threads

QThread will notifiy you via a signal when the thread isstarted(), finished(), and terminated(), or you can use isFinished() and isRunning() to query the state of thethread.

You can stop the thread by calling exit() or quit(). In extreme cases, you may want toforcibly terminate() anexecuting thread. However, doing so is dangerous and discouraged.Please read the documentation for terminate() and setTerminationEnabled()for detailed information.

From Qt 4.8 onwards, it is possible to deallocate objects thatlive in a thread that has just ended, by connecting the finished() signal to QObject.deleteLater().

Use wait() to block the callingthread, until the other thread has finished execution (or until aspecified time has passed).

The static functions currentThreadId() and currentThread() return identifiersfor the currently executing thread. The former returns a platformspecific ID for the thread; the latter returns a QThreadpointer.

To choose the name that your thread will be given (as identifiedby the command ps -L on Linux, for example), you can callsetObjectName() beforestarting the thread. If you don't call setObjectName(), the name givento your thread will be the class name of the runtime type of yourthread object (for example, 'RenderThread' in the case ofthe Mandelbrot Example, asthat is the name of the QThread subclass). Note that this iscurrently not available with release builds on Windows.

QThread also provides static, platform independent sleepfunctions: sleep(), msleep(), and usleep() allow full second, millisecond,and microsecond resolution respectively.

Note:wait() and thesleep() functions should beunnecessary in general, since Qt is an event-driven framework.Instead of wait(), considerlistening for the finished()signal. Instead of the sleep()functions, consider using QTimer.

{Mandelbrot Example}, {Semaphores Example}, {Wait ConditionsExample}

Type Documentation

QThread.Priority

ConstantValueDescription
QThread.IdlePriority0scheduled only when no other threads arerunning.
QThread.LowestPriority1scheduled less often than LowPriority.
QThread.LowPriority2scheduled less often than NormalPriority.
QThread.NormalPriority3the default priority of the operatingsystem.
QThread.HighPriority4scheduled more often than NormalPriority.
QThread.HighestPriority5scheduled more often than HighPriority.
QThread.TimeCriticalPriority6scheduled as often as possible.
QThread.InheritPriority7use the same priority as the creating thread.This is the default.

Method Documentation

QThread.__init__ (self, QObjectparent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a new QThread to manage anew thread. The parent takes ownership of the QThread. The thread does not begin executinguntil start() is called.

See alsostart().

QThread QThread.currentThread ()

Returns a pointer to a QThread whichmanages the currently executing thread.

int QThread.currentThreadId ()

int QThread.exec_ (self)

Enters the event loop and waits until exit() is called, returning the value thatwas passed to exit(). The valuereturned is 0 if exit() is calledvia quit().

Qt Thread Safe Signal Slot Adapter

This function is meant to be called from within run(). It is necessary to call this functionto start event handling.

See alsoquit() andexit().

QThread.exit (self, int returnCode = 0)

After calling this function, the thread leaves the event loopand returns from the call to QEventLoop.exec(). The QEventLoop.exec() function returnsreturnCode.

By convention, a returnCode of 0 means success, anynon-zero value indicates an error.

Note that unlike the C library function of the same name, thisfunction does return to the caller -- it is event processingthat stops.

No QEventLoops will be started anymore in this thread untilQThread.exec() has been calledagain. If the eventloop in QThread.exec() is not running then thenext call to QThread.exec() willalso return immediately.

See alsoquit() andQEventLoop.

int QThread.idealThreadCount ()

bool QThread.isFinished (self)

See alsoisRunning().

bool QThread.isRunning (self)

See alsoisFinished().

QThread.msleep (int)

See alsosleep() andusleep().

Priority QThread.priority (self)

Returns the priority for a running thread. If the thread is notrunning, this function returns InheritPriority.

This function was introduced in Qt 4.1.

See alsoPriority, setPriority(), and start().

QThread.quit (self)

See alsoexit() andQEventLoop.

QThread.run (self)

The starting point for the thread. After calling start(), the newly created thread callsthis function. The default implementation simply calls exec_().

You can reimplement this function to facilitate advanced threadmanagement. Returning from this method will end the execution ofthe thread.

See alsostart() andwait().

QThread.setPriority (self, Prioritypriority)

This function sets the priority for a running thread. Ifthe thread is not running, this function does nothing and returnsimmediately. Use start() to starta thread with a specific priority.

The priority argument can be any value in theQThread.Priority enum except forInheritPriorty.

The effect of the priority parameter is dependent on theoperating system's scheduling policy. In particular, thepriority will be ignored on systems that do not supportthread priorities (such as on Linux, seehttp://linux.die.net/man/2/sched_setscheduler for moredetails).

This function was introduced in Qt 4.1.

See alsoPriority, priority(), and start().

QThread.setStackSize (self, int stackSize)

See alsostackSize().

QThread.setTerminationEnabled (bool enabled = True)

Qt Thread Safe Signal Slot

Enables or disables termination of the current thread based onthe enabled parameter. The thread must have been started byQThread.

When enabled is false, termination is disabled. Futurecalls to QThread.terminate()will return immediately without effect. Instead, the termination isdeferred until termination is enabled.

When enabled is true, termination is enabled. Futurecalls to QThread.terminate()will terminate the thread normally. If termination has beendeferred (i.e. QThread.terminate() was called withtermination disabled), this function will terminate the callingthread immediately. Note that this function will not returnin this case.

See alsoterminate().

QThread.sleep (int)

See alsomsleep() andusleep().

int QThread.stackSize (self)

Returns the maximum stack size for the thread (if set withsetStackSize()); otherwisereturns zero.

Qt Signal Slot Example

See alsosetStackSize().

QThread.start (self, Prioritypriority = QThread.InheritPriority)

This method is also a Qt slot with the C++ signature void start(QThread::Priority = QThread.InheritPriority).

Begins execution of the thread by calling run(). The operating system will schedulethe thread according to the priority parameter. If thethread is already running, this function does nothing.

The effect of the priority parameter is dependent on theoperating system's scheduling policy. In particular, thepriority will be ignored on systems that do not supportthread priorities (such as on Linux, seehttp://linux.die.net/man/2/sched_setscheduler for moredetails).

See alsorun() andterminate().

QThread.terminate (self)

Terminates the execution of the thread. The thread may or maynot be terminated immediately, depending on the operating system'sscheduling policies. Listen for the terminated() signal, or use QThread.wait() after terminate(), to besure.

When the thread is terminated, all threads waiting for thethread to finish will be woken up.

Warning: This function is dangerous and its use isdiscouraged. The thread can be terminated at any point in its codepath. Threads can be terminated while modifying data. There is nochance for the thread to clean up after itself, unlock any heldmutexes, etc. In short, use this function only if absolutelynecessary.

Termination can be explicitly enabled or disabled by callingQThread.setTerminationEnabled().Calling this function while termination is disabled results in thetermination being deferred, until termination is re-enabled. Seethe documentation of QThread.setTerminationEnabled()for more information.

See alsosetTerminationEnabled().

QThread.usleep (int)

See alsosleep() andmsleep().

bool QThread.wait (self, int msecs = ULONG_MAX)

  • The thread associated with this QThread object has finished execution (i.e. whenit returns from run()). Thisfunction will return true if the thread has finished. It alsoreturns true if the thread has not been started yet.
  • time milliseconds has elapsed. If time isULONG_MAX (the default), then the wait will never timeout (thethread must return from run()). Thisfunction will return false if the wait timed out.

This provides similar functionality to the POSIXpthread_join() function.

See alsosleep() andterminate().

QThread.yieldCurrentThread ()

Qt Signal Documentation

void finished ()

See alsostarted() andterminated().

void started ()

See alsofinished()and terminated().

void terminated ()

See alsostarted() andfinished().

PyQt 4.11.4 for X11Copyright © Riverbank Computing Ltd and The Qt Company 2015Qt 4.8.7