Using Qt Signals / Slots with Non-Qt Streams

I have done my due diligence, but cannot find the answer to this question:

How does the Qt signal / slot mechanism work with streams other than Qt?

In particular, is it safe to release a signal from non-Qt (e.g. TBB ) in order to be caught by a slot in my main event loop? Suppose I connect it to a queue in a queue explicitly? (I feel that indicating that the connection has been queued would be mandatory, is that correct?)

(As a side issue, I assumed that in the general case, Qt synchronization classes, such as QMutex , work through non-Qt streams. Is this correct?)

(As an explanatory remark, what I'm worried about is that the queue mechanism in the queue will not use security devices, such as mutexes, to add meta-callers to the main thread's event queue if it does not detect that the signal is emitted from another flow Qt.)

(Final addition: I can believe that since Qt mechanisms are implemented in terms of platform-specific primitives, in practice all the things I try to do will just work gracefully, but I am also interested in Qt provides any guarantees that these things they will work.)

+5
source share
1 answer

The documentation states:

Note. Qt-stream classes are implemented using native threading APIs; e.g. Win32 and pthreads. Therefore, they can be used with threads of the same API.

So, yes, Qt mutexes will work with other threads (if they also use the same native API).

The difference between Qt threads and other threads is that the Qt event loop will never be triggered in other threads, so it will not be able to receive and process any signals. However, if you run the event loop ( exec ), everything should work fine inside such a thread.

Signal-related functions, mainly processEvents and postEvent are considered thread safe:

Note. This feature is thread safe.

If the objects have the thread affinity correctly set (using the moveToThread method), you do not need to set the connection type explicitly, by default AutoConnection works as follows:

(default) If a signal is called on a thread that has a receiving object, then the behavior is the same as a direct connection. Otherwise, the behavior will be the same as in the queue.

This answer suggests that non-Qt threads should also be correctly identified using Qt methods - currentThread should return a QThread instance even for a QThread Qt thread, since it is just a wrapper on top of its own threads.

+9
source

Source: https://habr.com/ru/post/1211091/