【问题标题】:Qt QProcess Complains about QThread::Start , Thread Creation ErrorQt QProcess 抱怨 QThread::Start ,线程创建错误
【发布时间】:2013-04-04 15:42:38
【问题描述】:

我有一个非常简单的应用程序,它应该使用 QProcess 来进行一些系统控制。然后整个程序如下。每次我运行该应用程序时,它都会抱怨以下内容:

QThread::start: Thread creation error: Resource temporarily unavailable

我用 _POSIX_THREAD_THREADS_MAX 打印出一个进程的最大线程数,它打印出 64。我也可以在命令行上运行 QProcess 命令,没有任何问题。什么给了?

代码:

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

    // Get the command line parameter to turn wifi on or off
    QString wifiSwitch = argv[1];

    // Print the number of threads available
    qDebug() << "Single Process can spawn this many threads:" << _POSIX_THREAD_THREADS_MAX;

    // Switch based on the input and control wifi with systemctl
    if ( wifiSwitch == "on" ) {

        // Subprocess systemd
        QProcess controlWifi;
        controlWifi.start("systemctl start wiap.service");
        controlWifi.waitForFinished();

        // Grab the output and use it to determine whether we successfully turned on the wifi
        QString didTurnOnWifi = QString(controlWifi.readAll()).trimmed();
        controlWifi.close();

        // So if there is no error messages from the subprocess we were successful
        if ( didTurnOnWifi.length() == 0 ) {
            qDebug() << "SUCCESS";
            exit(0);
        }
        else {
            qDebug() << "FAILURE";
            exit(-1);
        }

    }
    else if ( wifiSwitch == "off" ) {

        // Subprocess systemd
        QProcess controlWifi;
        controlWifi.start("systemctl stop wiap.service");
        controlWifi.waitForFinished();

        // Grab the output and use it to determine whether we successfully turned on the wifi
        QString didTurnOnWifi = QString(controlWifi.readAll()).trimmed();
        controlWifi.close();

        // So if there is no error messages from the subprocess we were successful
        if ( didTurnOnWifi.length() == 0 ) {
            qDebug() << "SUCCESS";
        }
        else {
            qDebug() << "FAILURE";
        }

    }
    else {

        // No arguments
        qDebug() << "FAILURE: You didn't specify any command line arguments, call this program like './fluke-control-wifi on|of'";
        exit(-1);

    }

    return a.exec();
}

注意:我最近从 Qt 4.8.3 升级到 Qt 4.8.4,但这不应该破坏 QProcess。我也找不到错误报告。

【问题讨论】:

  • 在代码中什么时候报告消息?
  • 在这一行 --> controlWifi.start("systemctl stop wiap.service");

标签: qt qthread qprocess qtembedded


【解决方案1】:

尝试添加

if(!controlWifi.waitForStarted())
{
    qDebug("Error starting process\n");
    return;
}

就在调用开始之后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 2012-04-09
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    相关资源
    最近更新 更多