PDA

View Full Version : Checking progress efficiently | C++



Backslash
21st February 2008, 19:16
Hello everyone,

I'm working with a function that, for instance, does some task it it's own thread and takes the address of an int for progress. Like so:

void doSometing(int* progress)

So, While the function is off running, I need an efficient way to check the progress for completion so that I may process the next item. I was doing something like this:



while(progress < 100)
{
progressBar->value = progress
}


That just seems like a bit much. But if I only check it every, say, 10 seconds, I could conceivably be wasting 9 seconds for every item. So, is there an accepted way to do something like this?

Thanks for your time,
Backslash

jacek
21st February 2008, 19:40
You can use QTimer to do something every 10 seconds, but don't expect that you will be able to read that variable's value reliably, if you won't use some kind of synchronization.

Michiel
23rd February 2008, 17:16
Why don't you use the observer pattern (or signals and slots, which already work well with threads) to automatically notify the progress bar when there's a change?

Backslash
23rd February 2008, 21:58
Thanks for the help. Also, sorry about forgetting code tags.

I'm taking a look at the observer pattern, thanks for the tip.

-Backslash