Suppose if an object obj
belongs to a QThread T1
. Ideally being in Qhread T2
's function, obj
can't be 'pulled' from T1
to T2
. This is mentioned in moveToThread()
documentation:
Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread. There is one exception to this rule however: objects with no thread affinity can be "pulled" to the current thread.
This answer's point-3 suggests that actually it's a "lie-to-children". Because moveToThread(nullptr)
will make an object to be movable from other threads.
Is it an idiomatic way without side-effects?
void FunctionRunningInT2 (QObject& obj) // `obj` belongs to thread `T1`
{
obj.moveToThread(nullptr); // line-1 no event processing for obj!?
obj.moveToThread(T2); // line-2 is it OK ???
}
Add-on question: What will happen if any signal
is emitted on obj
between line-1 and line-2?
Rephrased: In case of obj.disconnect()
, it doesn't accept any signals afterwards. However, the signals pending before disconnect()
are still processed. Is it true for moveToThread(nullptr)
as well? Or will it discard the pending signals too?