it was simpler than what I thought :) , Qt is doing all the stuff for me, so when I launch multiple threads in a loop for example , Qt checks if this thread is available for work or not, if it is available it will get called, if not then the call will get Queued
example test that worked for me using QProcess and QThread:
main of desired file to be launched in a QProcess
//desiredFile.exe
int main(int argc, char *argv[])
{
int sum;
for(int i = 0;i < 1000000000;++i)
{
//intensive task to keep threads busy
sum += (i%2 == 0)? i:-i;
sum += (i%3 == 0)? i:-i;
sum += (i%4 == 0)? i:-i;
sum += (i%5 == 0)? i:-i;
}
qDebug() << sum;
}
main of the multi threaded program:
//main.cpp
#include <QCoreApplication>
#include "thread.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString path = QCoreApplication::applicationDirPath ();
path.append("/desiredFile.exe");
for(int i = 0;i < QThread::idealThreadCount();i++)
{
Thread *workerThread = new Thread(path);
workerThread->start();
}
return a.exec();
}
and the thread code
//thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include <QProcess>
#include <QString>
class Thread : public QThread
{
Q_OBJECT
public:
Thread(QString commandLine);
void run();
//signals:
//void resultReady(const QString &s);
private:
QString CL;//commandline
};
#endif // THREAD_H
//thread.cpp
#include "thread.h"
Thread::Thread(QString commandLine)
{
this->CL = commandLine;
}
void Thread::run()
{
QProcess mProcess;
mProcess.execute(this->CL,QStringList());
mProcess.waitForFinished();
mProcess.close();
}
hope this helps :)