![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
yield_timeslice(); |
Frank Drebin
Member #2,987
December 2002
![]() |
ok i got my server and client app working now but it seems they don't run parallel cause they both take up 100% cpu. |
Chris Katko
Member #1,881
January 2002
![]() |
yield_tiemslice() gives up any extra cpu time to any other program that needs it. But if no other program does, it will still use 100% (*). WIP 4.1.13 and later changed it though. (insert angry smiley) -----sig: |
Frank Drebin
Member #2,987
December 2002
![]() |
k so this seems to me exactly what i need! |
ReyBrujo
Moderator
January 2001
![]() |
Inside any of your cycles, either the main or the logic one. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
i putted it after the logic_counter-- in my logic one. |
ReyBrujo
Moderator
January 2001
![]() |
Those are two different things. A quick explanation first (I was told this at school 7 years ago, so I might be remembering this all wrong)... The CPU works in quantums. A quantum is 1 second long (in fact, it is much, much smaller, say 5 microseconds, but 1 second will work for the example). The CPU gives one quantum to your program, then to another, then to another and continues until all have gotten a quantum. Now, suppose your program needs to read from hd. It executes 0.5 seconds, and ask the hd to read something. Since hd accessing is much slower (suppose it needs 5 seconds), your program stops there. The other 9.5 seconds are lost because it is your quantum and your program is awaiting for the hd to answer. yield_timeslice will tell the CPU that the quantum is over, and so those 9.5 seconds will be given to another process. In fact is keeping the CPU busy. Sleep, in the other hand, will stop the execution of your program for that amount of time. When the CPU gives you a quantum, you let the quantum pass, sleeping. Since nothing is being executed in the CPU, the load of the CPU lowers. In other words, yield_timeslice will optimize your CPU usage. Sleep will give it more idling time. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
alrighty that sounds nice to me but why doesn't it work for me? |
ReyBrujo
Moderator
January 2001
![]() |
/* sys_directx_yield_timeslice: * Yields remaining timeslice portion to the system. */ static void sys_directx_yield_timeslice(void) { Sleep(1); } Under Windows, it just calls Sleep... it should work... -- |
Frank Drebin
Member #2,987
December 2002
![]() |
Quote: Under Windows, it just calls Sleep... so it works just as fine as you explained on unix? Quote: it should work... believe me yield_timesclice() has no effect but Sleep(1) does |
Elias
Member #358
May 2000
|
As someone said, in the 4.0.x versions, yield_timeslice does Sleep(0) (which has no effect as far as this is concerned) and not Sleep(1), which only 4.1.14 does. -- |
Cody Harris
Member #4,406
March 2004
![]() |
I've found that rest(1) works well, because on unix, sleep(1) is actually one whole second. --------------------------------- |
ReyBrujo
Moderator
January 2001
![]() |
Ah... that explains some... I took the code from 4.1.14. Microsoft said Sleep(0) is enough, but here we discovered that, under VB, you needed at least 5 to get it down. Explains why they are not similar. (Edited: For unix systems, it uses select). -- |
Cody Harris
Member #4,406
March 2004
![]() |
Isn't select() for I/O Multiplexing? (From Beej's Guide to Network Programming) --------------------------------- |
Frank Drebin
Member #2,987
December 2002
![]() |
eeeh so in allegro 4.0.x yield_timslice() is the same as Sleep(0) and in later versions it is Sleep(1) so this is the thing for me to go? |
ReyBrujo
Moderator
January 2001
![]() |
Can be used too: /* _unix_sysdrv_yield_timeslice: * Yields remaining timeslice portion to the system */ void _unix_yield_timeslice(void) { struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 1; select(0, NULL, NULL, NULL, &timeout); } Descriptor 0 is stdin. It waits for an input key for 1 millisecond. -- |
Cody Harris
Member #4,406
March 2004
![]() |
usec is microseconds, not miliseconds... It waits one microsecond for a key input. There are 1,000 microseconds in a millisecond, and 1,000 milliseconds in a second. Thus, there are 1,000,000 microseconds in a second. I would have never thought of doing it like that... --------------------------------- |
Frank Drebin
Member #2,987
December 2002
![]() |
Frank Drebin said Quote: eeeh so in allegro 4.0.x yield_timslice() is the same as Sleep(0) and in later versions it is Sleep(1) so this is the thing for me to go? just give me a yes if i am correct. |
ReyBrujo
Moderator
January 2001
![]() |
Yes. -- |
orz
Member #565
August 2000
|
Generally, yield_timeslice() goes into your idle loop, if you have one. If you don't have an idle loop, instead sleep for 10 or 20 milliseconds after your rendering loop. I (and lots of other people) have complained about yield_timeslice() not effecting the %cpu usage listed in the windows task manager. Perhaps the 4.1.14 change fixed that? |
ReyBrujo
Moderator
January 2001
![]() |
Under VB, in example, I need a Sleep of 5 at least to drop CPU usage. Neither 0 nor 1 works. -- |
Chris Katko
Member #1,881
January 2002
![]() |
Quote: sleep(1) is actually one whole second. Sleep is milliseconds. If it slept it seconds it wouldn't be very useful. [edit] Added clicky, clarfication. -----sig: |
orz
Member #565
August 2000
|
Chris Katko: His statement was completely correct, though not entirely relevant. He was refering to the posix / iso / whatever standard function sleep() without the S capitalized, which takes an integer number of seconds to sleep for. Apparently some standards body thinks its useful. |
Kitty Cat
Member #2,815
October 2002
![]() |
If you want to reduce CPU usage, use rest(1) in your program. Under Windows and Linux, it'll do the same thing (give up the CPU for at least 1 millisecond). The function yield_timeslice has become a misnomer since it no longer does what its name implies, as well as changes the behavior of older programs. I don't think the fate of this function for 4.2 is, as of yet, known. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
so Sleep(1) or rest(1) ? ? ? |
ReyBrujo
Moderator
January 2001
![]() |
As you wish. Check which works for you and use it. -- |
|
1
2
|