简体   繁体   English

QObject 的线程亲和性作为已删除的 QThread 是否安全?

[英]Is it safe for a QObject's thread affinity to be a deleted QThread?

Consider the following code:考虑以下代码:

QThread *thread = nullptr;
QObject *object = nullptr;

void setup() {
    QThread *thread = new QThread;
    QObject *object = new QObject;
    object->moveToThread(thread);
    thread->start();
}

void cleanup() {
    thread->exit(0);
    thread->wait(1000);
    
    if (!thread->isFinished()) {
        // handle error condition
    }
    
    delete thread;
}

void postEventToThread() {
    if (!object) { return; }
    QEvent *event = new QEvent(QEvent::User);
    qApp->postEvent(object, event);
}

If postEventToThread() is called after setup() and before cleanup() , then everything should work properly and an event will get posted to object 's event queue and processed in thread .如果postEventToThread()setup()之后和cleanup()之前被调用,那么一切都应该正常工作,一个事件将被发布到object的事件队列并在thread中处理。

However, what happens if cleanup() is called before postEventToThread() ?但是,如果在postEventToThread()之前调用cleanup()会发生什么?

In that case object still exists and is a valid object, but its thread has been deleted.在这种情况下, object仍然存在并且是有效的 object,但其线程已被删除。 Does object automatically start behaving as though it has no thread affinity? object是否会自动开始表现得好像它没有线程关联? Or is this an error?或者这是一个错误?

I tested it out and it doesn't seem to print any warnings, cause any crashes, or otherwise behave badly.我对其进行了测试,它似乎没有打印任何警告、导致任何崩溃或以其他方式表现不佳。 But I want to check and make sure that deleting an object's thread (but not the object itself) and posting an event to it is allowed in Qt before I commit to it.但我想检查并确保删除对象的线程(但不是 object 本身)并在 Qt 中允许向其发布事件,然后再提交。

QThread starts the event loop by calling exec() and runs a Qt event loop inside the thread. QThread 通过调用 exec() 启动事件循环并在线程内运行 Qt 事件循环。

However, what happens if cleanup() is called before postEventToThread()?但是,如果在 postEventToThread() 之前调用 cleanup() 会发生什么?

thread is deleted and it's event loop exits, all event processing for object stops. thread被删除并且它的事件循环退出, object的所有事件处理停止。

Does object automatically start behaving as though it has no thread affinity? object 是否会自动开始表现得好像它没有线程关联? Or is this an error?或者这是一个错误?

In this case object is no longer associated with any thread.在这种情况下object不再与任何线程关联。 You can call it's methods from other threads, eg main thread of application.您可以从其他线程调用它的方法,例如应用程序的主线程。 Methods called directly on will execute in the thread that calls the method.直接调用的方法将在调用该方法的线程中执行。

But I want to check and make sure that deleting an object's thread (but not the object itself) and posting an event to it is allowed in Qt before I commit to it.但我想检查并确保删除对象的线程(但不是 object 本身)并在 Qt 中允许向其发布事件,然后再提交。

It's not disallowed but does not make sense.这不是不允许的,但没有意义。 As I've wrote before, since after object 's thread is deleted, all event processing for object stops.正如我之前所写的,因为在object的线程被删除之后, object的所有事件处理都会停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM