Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep UI responsive when CPU load is 100% (Mainly using C++ and Qt)?

I'm facing a problem between where I need to keep my UI (and the full OS) responsive in multi-threaded application.

I'm developing an application (c++ and Qt based) which received and transform lot of video frame from multiple stream at the same time.

Each stream is retrieved, transformed and rendered in its own separate worker thread (using DirectX). That means I'm not using the default GUI thread to render the frame.

On a powerful computer I have no problem because the cpu can process all data and keep time for the GUI thread to process user request. But on a old computer, it doesn't work, the CPU is used at 100% to process my data, and UI is lagging, it may takes 10 seconds before a button click become processed.

I would like to keep my UI responsive. In fact, I want my worker thread works only if there is no others action to do. I tried to change the worker thread priority to low, but it doesn't work. I also tried a sleep(10) in the worker thread, but because I can have lots of threads, they don't fall in sleep at the same time, so it's not working either.

What is the best way to keep an UI responsive in that case (whatever the toolkit use)?

like image 493
SaiyanRiku Avatar asked Jul 17 '12 09:07

SaiyanRiku


1 Answers

can't add my comments on above list so I've to add my few cents here:

  • if You want OS more responsive then make sure you don't consume too much RAM and start process in lower priority - afaik thread priorities are taken into account only when OS has to decide which thread from process should be run, but whole process still works on 100% cpu when other processes from system are taken into account
  • make sure not to run too many threads, good solution is to create as many threads that use 100% cpu as there are cores, if you want more then use multitasking techniques

One thing to check - how You do video display? Do you make sure your display rate (data from streams) matches display card's refresh rate? When You have data to display do You notify main thread about need to update screen (better solution) or You force frame display from each thread (bad solution)?

like image 52
Piotr Tkaczyk Avatar answered Oct 14 '22 16:10

Piotr Tkaczyk