PDA

View Full Version : Execute a QProcess instead of a QThread



peert@envysys.com
5th March 2019, 00:19
My application is written to run using a 3rd party DLL. The 3rd-party DLL is not thread safe. The application works as expected when each of (900+) threads execute one after another. When threads execute concurrently to QThreadPool::maxThreadCount - the application throws exceptions. I considered working around the limitation by executing each thread instead of within the context of a single application process but as a QProcess application. So for 10 tasks, the application would launch 10 QProcess processes (each QProcess working a single task).

Note, regarding the application - the application can open 100s of task threads. I understand it is necessary to implement a processing throttle which mimics QThreadPool::maxThreadCount.

In the spirit of "what can be done" are there thoughts / guidance on strategy to implement QProcess (or other approach) with Qt 5.12? If there is an example starter project - would greatly appreciate it.

Very best regards,

Tim Peer

Cruz
9th March 2019, 09:13
Hi Tim,

this is an interesting little grit. Walking the 100s of QProcesses way sounds theoretically viable, but you always only know after you tried. :)
I wonder why you would need to throttle the performance. You can spawn as many processes as you want and they will be distributed over the available cores by the underlying scheduler. What more needs to be done here?

As an alternative to consider, are you able to identify the place of the call into the DLL that is not thread safe? You could protect that call with a QMutex and then still use a QThreadPool or QtConcurrent or any thread based approach.

Bye,
Cruz

anda_skoa
9th March 2019, 09:37
Well, one option would be to stay close to your current code and simply have each QRunnable of your thread pool run a QProcess.

So if your current runnable code looks like this



void MyRunnable::run()
{
some3rdPartyFunction(param1, param2, param3);
}

then a QProcess based one would be a bit like this


void MyRunnable::run()
{
QProcess process;
process.start("helperExecutable", QStringList() << param1 << param2 << param3);
process.waitForFinished(-1);
}

with "helperExecutable" taking the commandline arguments to call some3rdPartyFunction in its main().

Cheers,
_