|
-
March 2nd, 2004, 08:45 AM
#16
thanks john
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 ?
-
March 2nd, 2004, 09:35 AM
#17
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.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 10:31 AM
#18
-
March 2nd, 2004, 10:35 AM
#19
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:
Code:
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...
Code:
UINT ThreadFunc(LPVOID *pvParam)
{
while(true)
{
// Do many calculations
::Sleep(0);
}
return 0;
}
-
March 2nd, 2004, 12:03 PM
#20
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!
Last edited by defiler_z; March 2nd, 2004 at 12:07 PM.
-
March 2nd, 2004, 12:14 PM
#21
This will solve your problem:
http://msdn.microsoft.com/library/de...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.
-
March 2nd, 2004, 01:45 PM
#22
Re: thanks!
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 ...
-
March 3rd, 2004, 12:22 AM
#23
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 ..
-
March 3rd, 2004, 03:35 AM
#24
Thank you all !
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|