我有一个非常简单的应用程序,它应该使用 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 .. 我也找不到错误报告。