#[repr(C)]pub struct CxxQtThread<T>where
T: Threading,{ /* private fields */ }
Expand description
A threading helper which is created from a QObject that implements Threading.
This allows for queueing closures onto the Qt event loop from a background thread as CxxQtThread implements Send.
When the Rust thread needs to update a value in the QObject it can then queue a closure to the thread. This closure will be executed on the thread the QObject lives in while holding a lock on the Rust object. Updating the QObject is then thread-safe.
See the Threading example for more information.
Implementations§
Source§impl<T> CxxQtThread<T>where
T: Threading,
impl<T> CxxQtThread<T>where
T: Threading,
Sourcepub fn queue<F>(&self, f: F) -> Result<(), ThreadingQueueError>
pub fn queue<F>(&self, f: F) -> Result<(), ThreadingQueueError>
Queue the given closure onto the Qt event loop for this QObject
The first argument of the closure is a pinned mutable reference to the QObject. With this parameter, you can then update the QObject to reflect any state changes that have occured in the background thread.
Sourcepub fn is_destroyed(&self) -> bool
pub fn is_destroyed(&self) -> bool
Checks whether the associated QObject
has been destroyed.
This method only confirms if the QObject
has already been destroyed.
It does not guarantee that the QObject
remains alive for any
subsequent operations. There is a potential race condition when using
is_destroyed()
before calling queue
. Specifically, the QObject
may
be destroyed after the check but before the queue
call.
For example:
if !thread.is_destroyed() {
thread.queue(/*...*/).unwrap();
}
In this scenario, the QObject
might be destroyed between the
is_destroyed
check and the queue
invocation, resulting in a panic.
To handle such cases safely, it is recommended to call queue(...).ok()
directly without checking is_destroyed()
. This approach allows you to
handle the potential failure gracefully without risking a panic.
However, is_destroyed()
can still be useful in scenarios where you
need to control loops or perform cleanup operations based on the
destruction status of the QObject
. For instance:
while !thread.is_destroyed() {
thread.queue(/*...*/).ok();
}