Question

well as I think the question is pretty naive! , but I couldn't find answer anywhere

the question: how to pass order to a SPECIFIC thread "thread 3 for example"

the only thing that I find getting info about threads is QThread::idealThreadCount() which returns 12 on my I7 3930K CPU "which is correct as 6 cores with 2 virtual threads per core"

now how to use this 12 threads .. I have 12 threads.... then what?? how to tell the thread with ID from 1 to 12 to do a specific job

edit: assume I want to use the 12 threads,each thread will do a different QProcess

so thread 1 do process C:\process1.exe, thread 2 do process C:\process2.exe

and so on ..., if I know the thread ID I will have full control ,

but using a subclass like MyThread = new MyThread(QString(process)); doesn't tell me which thread is this...

Was it helpful?

Solution

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top