Qt QProcess жалуется на QThread::Start , ошибка создания потока

У меня есть очень простое приложение, которое должно использовать 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.4 с Qt 4.8.3, но это действительно не должно сломать QProcess. Я также не могу найти отчет об ошибке для этого.


person PhilBot    schedule 04.04.2013    source источник
comment
Когда в коде сообщается сообщение?   -  person Min Lin    schedule 04.04.2013
comment
В этой строке --› controlWifi.start(systemctl stop wiap.service);   -  person PhilBot    schedule 04.04.2013


Ответы (1)


Попробуйте добавить

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

сразу после вызова, чтобы начать.

person TheDarkKnight    schedule 25.04.2013