QThread:最简单代码的竞争条件

时间:2016-09-24 18:51:58

标签: c++ qt qt5 valgrind qthread

调试应用程序我工作时发现一些奇怪的竞争条件显然不是由我们的代码引起的。 使用以下代码进行测试Helgrind会在几秒钟内报告超过5000种潜在竞争条件。

这里是代码:

#include <QApplication>
#include <QThread>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QThread thread;
    thread.start();

    return app.exec();
}

这是hellgrind报告的最初部分:

==9856== Helgrind, a thread error detector
==9856== Copyright (C) 2007-2015, and GNU GPL'd, by OpenWorks LLP et al.
==9856== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9856== Command: /home/gianks/NetBeansProjects/ThreadTest/dist/Debug/GNU-Linux/ThreadTest
==9856== 
==9856== ---Thread-Announcement------------------------------------------
==9856== 
==9856== Thread #1 is the program's root thread
==9856== 
==9856== ---Thread-Announcement------------------------------------------
==9856== 
==9856== Thread #2 was created
==9856==    at 0x6041B1E: clone (clone.S:74)
==9856==    by 0x6852189: create_thread (createthread.c:102)
==9856==    by 0x6853EC3: pthread_create@@GLIBC_2.2.5 (pthread_create.c:679)
==9856==    by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571DB7: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during read of size 4 at 0x599E9B0 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD30: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856== 
==9856== This conflicts with a previous write of size 4 by thread #2
==9856== Locks held: none
==9856==    at 0x556A977: QBasicMutex::lockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856==  Address 0x599e9b0 is in the Data segment of /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during write of size 8 at 0xBF41830 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD3A: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856== 
==9856== This conflicts with a previous read of size 8 by thread #2
==9856== Locks held: none
==9856==    at 0x556A9A5: QMutex::lock() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856==  Address 0xbf41830 is 112 bytes inside a block of size 168 alloc'd
==9856==    at 0x4C2F50F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x556D793: QThread::QThread(QObject*) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F96B2: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5D4: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x63F2D08: QGuiApplication::QGuiApplication(QGuiApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Block was alloc'd by thread #1
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during read of size 8 at 0xBF41830 by thread #2
==9856== Locks held: none
==9856==    at 0x556A92D: QBasicMutex::lockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856== 
==9856== This conflicts with a previous write of size 8 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD3A: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Address 0xbf41830 is 112 bytes inside a block of size 168 alloc'd
==9856==    at 0x4C2F50F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x556D793: QThread::QThread(QObject*) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F96B2: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5D4: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x63F2D08: QGuiApplication::QGuiApplication(QGuiApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Block was alloc'd by thread #1

真的有错误吗?

注意:我也尝试使用排队连接调用start,效果相同,因此在其他线程不是解决方案之前启动事件循环。

由于

2 个答案:

答案 0 :(得分:4)

helgrind不理解Qt本身内部使用的原子。你需要压制那个警告(并忽略它)。拿这个:

https://github.com/KDE/kde-dev-scripts/blob/master/kde.supp

--suppressions=/path/to/kde.supp添加到helgrind命令行。

有关在Qt应用程序中使用helgrind的更多信息,请查看this blog post

(旁注:破坏仍在运行的QThread会使你的程序崩溃,所以一定要在从main返回之前加入它。)

答案 1 :(得分:0)

根据Qt docs http://doc.qt.io/qt-5/qthread.html

,您没有以正确的方式使用QThread

您需要子类化QThread并重新实现run(),或者您可以使用QObject :: moveToThread()将它们移动到线程来使用worker对象。