To Kill a Battery

I see this time and time again.  It’s a practice that you don’t want to do in a a mobile program or any other program for that matter. In multithread programming a deveoper will sometimes want to stop execution of a thread until a certain event occurs or some other task is complete and will write something like the following to halt execution.

while(taskNotDone)
{
//Do nothing here
}

 

The problem with this code is it unnecessarily uses CPU bandwidth.  Most desktop applications spend a majority of their time waiting on some event to occur (waiting on a user to press a key, waiting on a file to load, waiting on a network event to happen).  When the above code is used the CPU is burning cycles when it could have been halted waiting for something to occur or giving up those cycles to another more constructive task.  On a Windows Mobile device this type of coding pattern will lead to the CPU not going into a lower power state when it could and unnecessarily causes power to be asserted across the memory bus.

Instead of using the above pattern you could join threads (which will cause one thread to wait for another to finish executing) or use events, mutexes, and semaphores to block a thread until it is signaled by another thread to continue.  Within the course of the next few weeks I will cover some of these techniques.  In the mean time you may wish to read the article I published on Power Management.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.