Click to See Complete Forum and Search --> : AfxBeginThread takes 100% CPU Usage
asaf a
March 2nd, 2004, 04:22 AM
i have a thread in my program that always run as long as the program runs ( runs all the time in loop ).
i operate the Thread by the command AfxBeginThread().
i checked the cpu usage of the thead in the task manager, and i saw that the thread takes 100% cpu usage , is that OK? how can i make the CPU usage lower ? ( i tried to switch the priority parameter of the AFXBeginThread function but it doesn't change the CPU usage).:(
John E
March 2nd, 2004, 06:09 AM
Do you mean that it goes up to 100% briefly while AfxBeginThread() is starting or that it stays permanently at 100% once it's started?
asaf a
March 2nd, 2004, 06:18 AM
thanks for answering john.
it Stays permanently on 100% once it started.
i even checked it when the thread function content is empty , it still takes 100% CPU Usage,so the problem is not the code inside the thread function it's the thread itself.:confused:
John E
March 2nd, 2004, 06:37 AM
Which version of AfXBeginThread() are you calling? The one that takes a pointer to a function or the one that takes a pointer to a runtime class?
John E
March 2nd, 2004, 06:40 AM
Hold it, I just noticed something....
Originally posted by asaf a
i have a thread in my program that always run as long as the program runs ( runs all the time in loop ).What do you mean here? That AfxBeginThread() is being called from within a loop or that the loop is running within the thread?
asaf a
March 2nd, 2004, 07:01 AM
thanks!
the loop is inside the thread , the AfxBeginThread is being called only once.
i use the one with the function pointer not the Runtime class.
i believe it is wrong for the cpu to behave like that ,is it ?
John E
March 2nd, 2004, 07:18 AM
Originally posted by asaf a
i believe it is wrong for the cpu to behave like that ,is it ?Yes, of course. But now I don't understand something you said earlier. You said...
Originally posted by asaf a
i even checked it when the thread function content is empty , it still takes 100% CPU UsageAre you saying that this still happens when the thread function is entirely empty? In other words there is no code running in it at all (not even the loop). As soon as it gets called, it immediately returns? Or does it still enter the loop?
NoHero
March 2nd, 2004, 07:22 AM
you mean, you got a thread looks like this?
int WINAPI ThreadProc( LPVOID lpParam )
{
while ( 1 == 1 )
{
// Some code
}
}
?? If you use something like this: take a timer with uiElapse = 1 or something higher ... it doesn't take the whole cpu usage ...
asaf a
March 2nd, 2004, 07:38 AM
i didn't make myself clear enough , sorry.
when the thread function is empty i dont have that problem, when i add the "While (TRUE)" loop inside the thread function the cpu jumps to 100% ( even when the while loop is empty of code ).
gstercken
March 2nd, 2004, 07:45 AM
The 100% CPU usage you see for the thread doesn't mean that the thread uses 100% of the total CPU time, but 100% of the time slices it gets assigned by the task scheduler. It's an obvious value for a thread which runs in a tight loop.
mr.speed
March 2nd, 2004, 07:49 AM
It is normal that the "while( true )" code to take your CPU to 100%. That is not an empty code. Your CPU does make alot or comparisons ( ( true == true ) ? ) and it makes them very fast.
You may try to add some Sleep(500) in your loop. That will give to your CPU some rest (in case you don't want your code can wait sometimes).
asaf a
March 2nd, 2004, 07:56 AM
thanks mr.speed
, but the empty while loop is only for the example . in my program i have lots and lots of comparisons and lot of work inside the while loop and still i get 100% usage.
Thanks gstercken,
Now i understand it better, i had a felling it doesn't take all CPU usage , since i can work with other MS programs (Word,excel,etc. ) while my program runs.
is there a way to see the cpu usage of the entier computer , so i can see the cpu usage that my program takes ?
John E
March 2nd, 2004, 08:03 AM
Originally posted by asaf a
iwhen the thread function is empty i dont have that problem, when i add the "While (TRUE)" loop inside the thread function the cpu jumps to 100% ( even when the while loop is empty of code ). It's the tight loop that is causing this problem, not AfxBeginThread(). Any tight loop is bound to soak up processor cycles, even if the loop isn't actually doing anything (in fact, especially if the loop isn't doing anything!) Running it in its own thread won't alter that situation
asaf a
March 2nd, 2004, 08:10 AM
as i said before , the tight loop is only for example to check the code with/without the loop . originally , there is alot of work inside the thread.
gstercken ,
if the computer is only showing the time slices it gets assigned by the task scheduler slices , why doesn't he show other programs time slices that run at the same time ? , why only my program time slices ?
John E
March 2nd, 2004, 08:23 AM
Originally posted by asaf a
as i said before , the tight loop is only for example to check the code with/without the loop . originally , there is alot of work inside the thread.Okay, but you also said before that the while loop is running continuously throughout the whole time that your program runs. Unless you provide some means of producing idle time (i.e. relinquishing your grip on the processor from time to time) you will end up having a tight loop, which is why you're seeing the problem that you're seeing. It doesn't matter whether the loop is doing a lot of work or only a little work. If it does a lot of work it will just loop more slowly but it will still be looping continuously.
asaf a
March 2nd, 2004, 08:45 AM
Now i got you! , sorry , i didnt understand you before.
Can't i make a thread that loop all the time and waits for data ( for example : always ready to receive data from Com1 )? how do i make idle time inside the loop ?
John E
March 2nd, 2004, 09:35 AM
I'm loathe to suggest this but a "quick and dirty" way is to use something like "Sleep(60)" at the beginning or end of the loop (you can vary the actual figure to suit yourself). It's a dirty way out because never-ending loops are generally considered a bad thing to use in Windows programming. I don't know what exactly you're trying to do but I'll bet there's a better way to do it than running it in a loop for the entire duration of the program.
Andreas Masur
March 2nd, 2004, 10:31 AM
[Moved thread]
Andreas Masur
March 2nd, 2004, 10:35 AM
Well...I skimmed through the previous replies, however, if you are using a simple while loop in your thread it most-likely will show the 100% CPU disregarding how many things you are doing in between.
For example:
UINT ThreadFunc(LPVOID *pvParam)
{
while(true)
{
// Do many calculations
}
return 0;
}
This might still give you the 100% CPU...in order to avoid that, simply force a context switch...
UINT ThreadFunc(LPVOID *pvParam)
{
while(true)
{
// Do many calculations
::Sleep(0);
}
return 0;
}
defiler_z
March 2nd, 2004, 12:03 PM
warning: my english is not very good, so if I've made mistakes sorry!
Well, it depends on what you want to do with your program. I will suggest you that: leave your main function in an infinite loop, and procces other functions in separate threads. Something like this:
// I will use _beginthread() function
/*global*/ int repeat = 1;
void proccesSomething()
{
do {
Sleep(500);
}while(repeat);
}
void main()
{
// do something(for example listening to port)
_beginthread(listenFunc, 0, /*data*/);
proccesSomething();
// clean up
}
void MyFunc()
{
//do something
//_beginthread(Myfunc2, 0, /*data*/);
// and so on
_endthread();
}
void listenFunc()
{
//do something(loop for example)
if(/*variable == 1*/)
{
_beginthread(MyFunc, 0, /*data*/);
}
//continue listening
_beginthread(listenFunc, 0, /*data*/);
_endthread();
}
I hope that will help you!
smg123
March 2nd, 2004, 12:14 PM
This will solve your problem:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_idle_loop_processing.asp
I had the same problem with an unresponsive GUI - putting the same message dispatch loop in the middle of my long process fixed the problem.
NoHero
March 2nd, 2004, 01:45 PM
Originally posted by asaf a
i didn't make myself clear enough , sorry.
when the thread function is empty i dont have that problem, when i add the "While (TRUE)" loop inside the thread function the cpu jumps to 100% ( even when the while loop is empty of code ).
sry ... I tried to say that you should use a timer instead ... but forget it ...
simpleman
March 3rd, 2004, 12:22 AM
hi
i am simpleman
1. the easyest way : Using Sleep(1);
i think the Time in Sleep() mentioned above is too much.
2. more hard way : Using Event
i think you receive some data from socket, if then make an event for the each socket and using WaitForSingleObject() or WaitForMultipleObjects(). i think you can get the network event if you use WSAEnumNetworkEvents().
Good luck to you ..
asaf a
March 3rd, 2004, 03:35 AM
Thank you for all your help , i solved the problem.
simpleman :
Thanks , as for now i'm using the solution you suggested, i put Sleep(1) in the loop and the CPU usage decreased from 100% to 3% in full calculations inside loop. thanks.
John E and Andreas Masur : thanks.
smg123 : thank you for the link , now i know that there is idle processing and i will implement it in my program in the future.
Thanks you all!
:)
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.