2

在我的程序中,我将 QThread 子类化,并run()像这样实现了虚拟方法:

void ManagerThread::run() {

    // do a bunch of stuff,
    // create some objects that should be handled by this thread
    // connect a few signals/slots on the objects using QueuedConnection

    this->exec(); // start event loop
}

现在,在另一个线程(我们称之为MainThread)中,我启动ManagerThread等待它的started()信号,之后我继续使用应该由ManagerThread. 但是,started()信号本质上run()是在调用之前发出的,所以根据线程调度,我会丢失一些来自MainThread的信号,因为事件循环还没有开始!

编辑:原来这不是问题,只是信号没有及时连接,但出于同样的原因)

我可以在打电话之前发出信号exec(),但这也是自找麻烦。

是否有任何确定/简单的方法可以知道事件循环已经开始?

谢谢!

EDIT2:(解决方案)

好吧,事实证明问题并不完全是我所说的。事件循环没有开始的事实不是问题,因为信号应该排队直到它开始。问题是,一些信号无法及时连接到被调用——因为started()信号在run()被调用之前发出。

解决方案在所有连接之后和 exec 之前发出另一个自定义信号。这样可以确保连接所有信号/插槽。

这是我的问题的解决方案,但不是线程标题的真正答案。我已经接受了确实回答了标题的答案。

我将所有代码留给那些好奇的人,解决方案是等待instance()方法中的另一个信号。


代码:

你们中的许多人都说我不能丢失信号,所以这是我的全班实现。我会将其简化为最基本的必需品。

这是接口ManagerThread

// singleton class
class ManagerThread: public QThread {

    Q_OBJECT

    // trivial private constructor/destructor

    public:
    static ManagerThread* instance();

    // called from another thread
    public:
    void doSomething(QString const& text);

    // emitted by doSomething,
    // connected to JobHandler whose affinity is this thread.
    signals:
    void requestSomething(QString const& text);

    // reimplemented virtual functions of QThread
    public:
    void run();

    private:
    static QMutex s_creationMutex;
    static ManagerThread* s_instance;
    JobHandler* m_handler; // actually handles the requests
};

一些相关的实现。创建线程的单例实例:

ManagerThread* ManagerThread::instance() {
    QMutexLocker locker(&s_creationMutex);
    if (!s_instance) {
        // start socket manager thread, and wait for it to finish starting
        s_instance = new ManagerThread();

        // SignalWaiter essentially does what is outlined here:
        // http://stackoverflow.com/questions/3052192/waiting-for-a-signal
        SignalWaiter waiter(s_instance, SIGNAL(started()));
        s_instance->start(QThread::LowPriority);
        qDebug() << "Waiting for ManagerThread to start";
        waiter.wait();
        qDebug() << "Finished waiting for ManagerThread thread to start.";
    }

    return s_instance;
}

重新实现设置信号/槽并启动事件循环的运行:

void ManagerThread::run() {
   // we are now in the ManagerThread thread, so create the handler
   m_handler = new JobHandler();

   // connect signals/slots
   QObject::connect(this,
                    SIGNAL(requestSomething(QString const&)),
                    m_handler,
                    SLOT(handleSomething(QString const&)),
                    Qt::QueuedConnection);

   qDebug() << "Starting Event Loop in ManagerThread";

   // SOLUTION: Emit signal here and wait for this one instead of started()

   this->exec(); // start event loop
}

将处理委托给正确线程的函数。这是我发出丢失信号的地方:

void ManagerThread::doSomething(QString const& text) {

    qDebug() << "ManagerThread attempting to do something";

    // if calling from another thread, have to emit signal
    if (QThread::currentThread() != this) {
        // I put this sleep here to demonstrate the problem
        // If it is removed there is a large chance the event loop
        // will not start up in time to handle the subsequent signal
        QThread::msleep(2000);  
        emit(requestSomething(text));
    } else {
       // just call directly if we are already in the correct thread
       m_handler->handleSomething(text);
    }
}

最后,MainThread如果事件循环没有及时启动,下面的代码将会失败:

ManagerThread::instance()->doSomething("BLAM!");

假设处理程序只是打印出它的文本,下面是成功运行时打印出的内容:

等待 ManagerThread 启动
完成等待 ManagerThread 线程启动。
在 ManagerThread 中启动事件循环
ManagerThread 试图做某事
BLAM!

以下是不成功运行时会发生的情况:

等待 ManagerThread 启动
完成等待 ManagerThread 线程启动。
ManagerThread 试图做某事
在 ManagerThread 中启动事件循环

显然,事件循环在信号发出后开始,并且 BLAM 从不打印。这里有一个竞争条件,需要知道事件循环何时开始才能修复它。

也许我错过了一些东西,问题是不同的......

如果您真的阅读了所有内容,非常感谢!呸!

4

4 回答 4

3

如果您正确设置连接,则不应丢失信号。但是如果你真的想在线程的事件循环开始时得到通知,你可以在调用之前尝试QTimer::singleShot()一下。它将在事件循环开始时交付,并且只交付一次。run()exec()

于 2011-03-10T16:06:15.280 回答
1

这是一个非问题。线程之间的信号是排队的(更具体地说,您需要将它们设置为在connect()调用中排队,因为线程之间的直接连接不安全)。

http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads

于 2011-03-10T16:01:46.397 回答
1

您可以查看QSemaphore线程之间的信号。插槽和信号更适合同一线程上的 ui 事件和回调。

编辑:或者,如果信号量不适用,您可以结合QMutex使用。QWaitCondition更多示例代码以了解您如何将 ManagerThread 与 MainThread 结合使用会有所帮助。

于 2011-03-10T15:52:25.007 回答
0

您可以在 ManagerThread 的构造函数中创建信号/插槽连接。这样,即使在调用 run() 之前,它们也肯定是连接的。

于 2011-03-10T17:27:49.753 回答