Write a function to cause a thread to wait
-
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.
-
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.
Dan_K wrote:
that the CPU Usage shoots up to 100 percent
No wonder: you're using the CPU, aren't you? That's not the best way to pause an application on a multitasking system. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.
You should upgrade your hardware. If you buy a dual-core system, it will become twice as efficient: the CPU load will be around 50%. Or go for a quad-core and see it sitting idle with a load of a mere 25%. If you don't like that idea, you should either use a sleep function or a timer. FWIW: you could do both to make absolutely sure. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.
Your CPU usage goes to 100% simply because you are actually doing something: the while loop and the checks are operations done by the CPU, and in your case without interruption, so the CPU is always busy.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Your CPU usage goes to 100% simply because you are actually doing something: the while loop and the checks are operations done by the CPU, and in your case without interruption, so the CPU is always busy.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Cedric Moonen wrote:
so the CPU is always busy
It's the ancient wise's rule: "keep the CPU busy and the brain sleeping..." :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.
-
Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.