簡體   English   中英

QThread:具有最簡單代碼的競爭條件

[英]QThread: race condition with the simplest code

調試我正在使用的應用程序時,我發現一些奇怪的競爭條件顯然不是由我們的代碼引起的。 使用以下代碼進行測試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

實際上有錯誤嗎?

注意:我也嘗試過使用排隊連接啟動,效果相同,因此在其他線程之前啟動事件循環不是解決方案。

謝謝

Helgrind不了解Qt本身內部使用的原子。 您需要禁止顯示該警告(並忽略它)。 獲取此:

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

並將--suppressions=/path/to/kde.supp到helgrind命令行。

有關在Qt應用程序中使用helgrind的更多信息,請查看此博客文章


(附帶說明:銷毀仍在運行的QThread會使您的程序崩潰,因此請確保在從main返回之前將其加入。)

您沒有按照Qt docs http://doc.qt.io/qt-5/qthread.html正確地使用QThread

您需要繼承QThread並重新實現run(),或者可以通過使用QObject :: moveToThread()將輔助對象移動到線程中來使用輔助對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM