PDA

View Full Version : Thread start overhead



^NyAw^
20th December 2017, 11:49
Hi,

I'm developing an application that have many threads. These threads were started and then it waits until all threads have finished the work.
I start all the threads using a loop and I have calculated that the time spent to start these threads is high. High means more ore less 25ms to start 18 threads. It seems not to much but this is my bootleneck.

My Idea is to use a QWaitCondition on every thread. Start the threads. As they do not have data to process will go to sleep waiting a signal to the wait condition.
When I need them to process I can cal "wakeOne" to let the thread go up an process. After it finish the process it will go to sleep again.

Could this work?

And, if this works, how can I wait that all threads have finished the work?

Note: There is a worker thread(do not have an event loop) that start all the other threads.

Thanks,

son
20th December 2017, 12:02
How do you measure thread initialization time?

Lesiok
20th December 2017, 12:23
And, if this works, how can I wait that all threads have finished the work?

Make a list with pointers to threads. After finishing worker sends a signal. In slot remove the worker from the list. If list empty done.

^NyAw^
20th December 2017, 12:29
Hi,



How do you measure thread initialization time?

Using QElapsedTimer


Make a list with pointers to threads. After finishing worker sends a signal. In slot remove the worker from the list. If list empty done.

Think on that the worker thread is a thread with a loop that starts all the other threads, so there is no event loop in this worker thread.

This is my approach


MyThread::run()
{
for (int i=0; i<qNumThreads; i++)
m_qThreadList[i]->start();

for (int i=0; i<qNumThreads; i++)
m_qThreadList[i]->wait();

//Get results
}


Thanks,

d_stranz
20th December 2017, 18:58
m_qThreadList[i]->wait();

Hmmm, isn't this a blocking function? If wait() doesn't return until the thread has finished, then your loop will not complete until all threads have finished, and MyThread::run() won't return either.

^NyAw^
22nd December 2017, 10:32
Hi,

Let me be a little more presice:


MyThread::run()
{
while(!bExit)
{
get_image_from_camera
for (int i=0; i<qNumThreads; i++)
m_qThreadList[i]->start();

for (int i=0; i<qNumThreads; i++)
m_qThreadList[i]->wait();

//Get results and emit some signal to let the GUI show results
}
}


"MyThread" is running in a loop until the user stops it.
The other Threads(ThreadList) are processing Threads that compute some image processing algorithms parallel, so I need to start them and wait for them to finish to get results.

The overhead seems to be on the thread start. The OS have to create a thread, stack, copy system variables, ...

So, my question was if I could start the threads but put them to sleep with a QWaitCondition. When the "MyThread" have captured a frame from the camera, set a boolean let the threads process it. Finally what I don't know how to wait until the threads have finished.

Something like this wastes CPU on MyThread


for (int i=0; i<qNumThreads; i++)
{
while (m_qThreadList[i]->isProcessing())
;
}


Maybe using another QWaitCondition that the threads could call "wakeOne()" ?


for (int i=0; i<qNumThreads; i++)
{
m_qThreadList[i]->m_qWaitCondition->wait();
//Here we know that thread "i" have finished its process
}


Could this work?

Thanks,