简体   繁体   English

Qt moveToThread和数据竞赛

[英]Qt moveToThread and data races

There is a class with one signal and slot: 有一个带有一个信号和插槽的类:

#ifndef SOME_CLASS_H
#define SOME_CLASS_H

#include <QObject>

class SomeClass : public QObject
{
    Q_OBJECT
public:
    explicit SomeClass(QObject *parent = 0) : QObject(parent) {
        connect(this, &SomeClass::valueChanged, this, &SomeClass::setValue);
    }

    void emitSignal() {
        emit valueChanged();
    }

signals:
    void valueChanged();
public slots:
    void setValue() {}
};

#endif // SOME_CLASS_H

Here is main file of program defining a singleton which creates instance of above defined class and puts it into separate thread: 这是定义单例的程序主文件,该单例创建上述定义的类的实例并将其放入单独的线程中:

#include <QCoreApplication>
#include <QThread>
#include <someclass.h>

class Singleton {
public:
    QThread t;
    SomeClass someClassObject;
    Singleton() {
        someClassObject.moveToThread(&t);
        t.start();
    }

    static SomeClass& getInstance() {
        static Singleton st;
        return st.someClassObject;
    }

    ~Singleton() {
        t.quit();
        t.wait();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    SomeClass& b = Singleton::getInstance();
    b.emitSignal();

    QThread::msleep(100);

    return a.exec();
}

The problem is that when I evaluate this program using some thread checking tools like ThreadSanitizer or Helgrind then several warnings are reported about data race conditions. 问题是,当我使用某些线程检查工具(例如ThreadSanitizer或Helgrind)评估该程序时,会报告一些有关数据争用情况的警告。 Example output from ThreadSanitizer: ThreadSanitizer的示例输出:

==================
WARNING: ThreadSanitizer: data race (pid=3827)
  Read of size 8 at 0x7d040000f6a0 by thread T1:
    #0 void QtPrivate::FunctionPointer<void (SomeClass::*)()>::call<void, void>(void (SomeClass::*)(), SomeClass*, void**) /usr/include/qt5/QtCore/qobjectdefs_impl.h:142 (exe+0x0000000a2d1c)
    #1 QtPrivate::QSlotObject<void (SomeClass::*)(), void, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) /usr/include/qt5/QtCore/qobject_impl.h:149 (exe+0x0000000a29b4)
    #2 QObject::event(QEvent*) ??:0 (libQt5Core.so.5+0x00000029022d)

  Previous write of size 8 at 0x7d040000f6a0 by main thread:
    #0 malloc ??:0 (exe+0x0000000426e9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028ef1c)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Location is heap block of size 8 at 0x7d040000f6a0 allocated by main thread:
    #0 malloc ??:0 (exe+0x0000000426e9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028ef1c)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Thread T1 'QThread' (tid=3829, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000046f6b)
    #1 QThread::start(QThread::Priority) ??:0 (libQt5Core.so.5+0x0000000928b7)
    #2 Singleton::getInstance() /tmp/project/main.cpp:16 (exe+0x0000000a1b3c)
    #3 main /tmp/project/main.cpp:30 (exe+0x0000000a19bf)

SUMMARY: ThreadSanitizer: data race /usr/include/qt5/QtCore/qobjectdefs_impl.h:142 void QtPrivate::FunctionPointer<void (SomeClass::*)()>::call<void, void>(void (SomeClass::*)(), SomeClass*, void**)
==================
==================
WARNING: ThreadSanitizer: data race (pid=3827)
  Write of size 1 at 0x7d040000f6b0 by thread T1:
    #0 free ??:0 (exe+0x000000042dfb)
    #1 QMetaCallEvent::~QMetaCallEvent() ??:0 (libQt5Core.so.5+0x00000028d3a4)

  Previous write of size 8 at 0x7d040000f6b0 by main thread:
    #0 malloc ??:0 (exe+0x0000000426e9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028eefe)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Location is heap block of size 4 at 0x7d040000f6b0 allocated by main thread:
    #0 malloc ??:0 (exe+0x0000000426e9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028eefe)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Thread T1 'QThread' (tid=3829, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000046f6b)
    #1 QThread::start(QThread::Priority) ??:0 (libQt5Core.so.5+0x0000000928b7)
    #2 Singleton::getInstance() /tmp/project/main.cpp:16 (exe+0x0000000a1b3c)
    #3 main /tmp/project/main.cpp:30 (exe+0x0000000a19bf)

SUMMARY: ThreadSanitizer: data race ??:0 free
==================
==================
WARNING: ThreadSanitizer: data race (pid=3827)
  Write of size 8 at 0x7d180000edc0 by thread T1:
    #0 operator delete(void*) ??:0 (exe+0x00000004392b)
    #1 QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) ??:0 (libQt5Core.so.5+0x000000269e0f)

  Previous write of size 8 at 0x7d180000edc0 by main thread:
    #0 operator new(unsigned long) ??:0 (exe+0x0000000432b9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028ef94)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Location is heap block of size 88 at 0x7d180000edc0 allocated by main thread:
    #0 operator new(unsigned long) ??:0 (exe+0x0000000432b9)
    #1 QMetaObject::activate(QObject*, int, int, void**) ??:0 (libQt5Core.so.5+0x00000028ef94)
    #2 main /tmp/project/main.cpp:31 (exe+0x0000000a19f2)

  Thread T1 'QThread' (tid=3829, running) created by main thread at:
    #0 pthread_create ??:0 (exe+0x000000046f6b)
    #1 QThread::start(QThread::Priority) ??:0 (libQt5Core.so.5+0x0000000928b7)
    #2 Singleton::getInstance() /tmp/project/main.cpp:16 (exe+0x0000000a1b3c)
    #3 main /tmp/project/main.cpp:30 (exe+0x0000000a19bf)

SUMMARY: ThreadSanitizer: data race ??:0 operator delete(void*)
==================

What does cause these race conditions? 是什么原因导致这些比赛条件?

Since the warnings are all originating in Qt internals related to signals, slots, and event loops, I think that the race conditions detected are false positives. 由于警告全部源自与信号,插槽和事件循环有关的Qt内部,因此我认为检测到的竞争条件是误报。 Does Helgrind detect the same race conditions? Helgrind是否检测到相同的比赛条件? Helgrind bugs with Qt are reported by some, ( http://www.kdab.com/~dfaure/helgrind.html ) and seem to be addressed recently. 一些人报告了Qt的Helgrind错误( http://www.kdab.com/~dfaure/helgrind.html ),并且似乎在最近得到了解决。

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

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