My code is rather long and convoluted at the moment (I am working on cleaning it up), and I don't think my question will require it being posted, but once I get it straightened out I'll be happy to post it. Just saying this since there are GIANT letters in this text box saying "You Must Show Us Your Code."
I'm communicating with an external device using a serial (COM) port on the Windows Vista operating system, in the C++ language. By creating and using a handle to a file (the COM port), I am successfully reading and writing to the communications port. So my question is NOT how to communicate with an external device.
The question arises from a problem with my program. I've used the wxWidgets toolkit to build a GUI for my program, and because the toolkit allows for the easy implementation of multiple threads, I decided to put the serial port code inside a separate thread then that where my main program resides, and to simply run the thread continuously, polling the COM port continuously. My problem was, for a certain amount of time after opening the program, the user is not connected to the serial port, and thus the code inside the thread was not executed (I had it inside an if statement, to only run if the user had connected to the serial port). And so, with the help of the users on the toolkit's forum, I learned of "Busy Waiting." Apparently if the while loop is empty (which is was, since the if statement was passed over), this "Busy Waiting" occurs and takes nearly 100% of the CPU! How terrible! What mystified me was that when the serial port code was executed, the CPU usage was minimal. This seemed absurd, until someone suggested that perhaps the serial port code on Windows had some sort of sleep statement built into it.
And so, after searching Google, I found this forum
As previously mentioned, my code is at the moment, a mess. But this document is where the inspiration for my code came from, and I'm sure is much clearer to read then mine:
Could anyone provide me with some insight, as to how executing the serial port code keeps the "Busy Waiting" from occurring?
Appreciate the advice!
This post has been edited by Robotics Guy: 20 March 2009 - 04:49 PM