Reputation: 2053
I currently have this code
#include <iostream>
#include <curl.h>
#include <windows.h>
#include "boost\timer.hpp"
int main(void)
{
CURL *curl;
CURLcode res;
boost::timer t;
int number = 1;
while (number == 1)
{
if(t.elapsed() > 10)
{
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
t.restart();
}
}
}
What i'd like it to do is continue execution of this program and never end until someone closes the window. I tried the aforementioned code, however CPU usage spiked to 25% on my quad core CPU.
So how do i continue the execution of the program and loop the code within the while without using so much CPU?
P.S 25% on a quad core means 100% cpu usage on a single core CPU.
Upvotes: 1
Views: 306
Reputation: 2984
Consider using a timer instead of doing such a tight loop. Alternatively you can put a System.Threading.Thread.Sleep(300) in between.
Someone would have to kill your process to close it, I cannot discern any window in your code.
If you are polling a website (it looks like you are doing something with Google there) then I would advise you to make a much larger interval! Not many a web-master would be happy to see such activity. It's more likely to be seen as a DOS-attack!
Any way, if there's a window, rather put this code in a timer delegate, otherwise start a timer and allow the user to exit your program somehow (maybe with Console.ReadKey() or so).
Upvotes: 0
Reputation: 65176
What you're currently doing is busy waiting. That is, even though your program doesn't need to do anything, it's still keeping that loop spinning, waiting for the timer. What you need to do is to execute a true sleep, which tells your operating system that the process doesn't need to do anything for the next 10 seconds.
One way to do a true sleep in boost is the boost::this_thread::sleep function.
Upvotes: 2
Reputation: 63005
Call SwitchToThread()
inside of your while
loop. (Sleep
is less than ideal, as it forfeits the current time slice even if no other thread needs it.)
Upvotes: 0
Reputation: 63320
You can boost::threads
to run it in it's own thread and then do a thread.join in the main thread to wait for it. If the other thread never ends, because of a while(true)
then you program will run until you close the window.
Upvotes: 0
Reputation: 363858
What you've implemented is called a busy wait and is considered very bad style. Use sleep
to suspend program execution for a short time, and write an eternal loop as:
for (;;)
or
while (true)
Upvotes: 1
Reputation: 46697
You can use Sleep(10000)
to pause program execution for approx. 10 seconds. You can drop the boost::timer
- just sleep 10 seconds in each loop iteration (Sleep is not as accurate, but for 10 seconds the inaccuracy should be negligible).
Your code is what is called a 'busy loop' - for the CPU it makes no difference whether you hang around in a tight loop without much work or do heavy computations. Both will use 100% of a CPU core because there's an neverending stream of instructions coming in. To use less, you need to relinquish execution for a while to let the OS execute other processes.
Upvotes: 3
Reputation: 11725
If need to slow it down with some sleep()
. Basically you need to put your thread to sleep to allow other processes to execute.
Upvotes: 1