QtCore QThreadPool在glibc 2.34下访问触发段错误求助
Qt与glibc 2.34的线程库兼容性问题
我是量子物理学家,并非专业工程师。我开发的多线程Qt5程序在Ubuntu 18.04 LTS上使用QThread和QtConcurrent库运行完全正常。在main()函数中,首次调用这些库是为了根据命令行参数settings.threads设置maxThreadCount。但该程序在Ubuntu 21.04和22.04 LTS上会直接崩溃,我推测原因是glibc库从2.33升级到2.34后对pthread相关机制做了大量修改,将libpthread并入了libc库。
崩溃发生在__libc_start_main中,栈跟踪如下:
Stack trace (most recent call last): #5 Object "", at 0xffffffffffffffff, in #4 Object "/.../.../master/engine/build/exe/main/debug/engine", at 0x55f2c6dfbc74, in _start #3 Object "/usr/lib/x86_64-linux-gnu/libc.so.6", at 0x7f0703bb2e3f, in __libc_start_main #2 Object "/usr/lib/x86_64-linux-gnu/libc.so.6", at 0x7f0703bb2d8f, in #1 Source "/.../.../master/engine/src/main/cpp/main.cpp", line 1727, in main [0x55f2c6dd80b6] 1725: 1726: >1727: QThreadPool::globalInstance()->setMaxThreadCount(state.settings.threads -1); 1728: auto threadCount = QThread::idealThreadCount(); 1729: qInfo() << qString(construQt); 1730: qInfo() << qString("------------------- | CPU Information | ------------------\nSystem has {} cpus\nRequesting {} threads\n", qString(threadCount), qString(state.settings.threads)); #0 Object "/usr/lib/x86_64-linux-gnu/libQt6Core.so.6.2.4", at 0x7f07043aea50, in QThreadPool::setMaxThreadCount(int) Segmentation fault (Address not mapped to object [0x8]) Segmentation fault (core dumped)
我试过将gcc从7升级到12编译,也通过apt把Qt5.14升级到Qt6.2.4,但都无济于事,且已按照Qt要求安装了所有依赖项。使用std::thread没有问题,因此我认为这是QThread与glibc 2.34之间的交互问题。
想问问社区有没有人遇到过Qt与libc 2.34之间的类似问题,尤其是和pthread库相关的情况。
相关代码片段:
class Engine : public QObject, public QRunnable { void run() override { qInfo() << "Say Anything"; } }; Engine *engine = new Engine(); QThreadPool *threadPool; if (QThreadPool::globalInstance()) { qInfo() << "Is the gloabalInstance there?"; } threadPool = QThreadPool::globalInstance(); threadPool->start(engine); // Will crash threadPool->setMaxThreadCount(state.settings.threads -1);
内容的提问来源于stack exchange,提问作者PeterJ
相关产品推荐
相关产品推荐

