Raised This Month: $ Target: $400
 0% 

while() cpu load 100%


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-14-2009 , 07:33   while() cpu load 100%
Reply With Quote #1

I have this code
PHP Code:
#include <amxmodx>
#include <engine>

public plugin_init( ) {
    
register_plugin"Reset Buttons""1.0""xPaw" );
    
    
register_event"HLTV""evNewRound""a""1=0""2=0" );
}

public 
evNewRound( ) {
    new 
iButton    = -1;
    new 
Float:flGametime get_gametime();
    new 
Float:flNextthinkFloat:flLTime;
    
    while( ( 
iButton find_ent_by_classiButton"func_button" ) != ) ) {
        
flNextthink entity_get_floatiButtonEV_FL_nextthink );
        
flLTime entity_get_floatiButtonEV_FL_ltime );
        
        if( 
flNextthink flLTime && flNextthink flGametime ) {
            
entity_set_floatiButtonEV_FL_nextthinkflLTime );
        }
    }

When it starts new round, server just freezes and cpu usage goes to 100% :\
__________________
xPaw is offline
--kml--
Senior Member
Join Date: Jan 2009
Old 05-14-2009 , 09:20   Re: while() cpu load 100%
Reply With Quote #2

oo maybe change new to static?

Code:
#include <amxmodx> 
#include <engine> 

public plugin_init( ) { 
    register_plugin( "Reset Buttons", "1.0", "xPaw" ); 
     
    register_event( "HLTV", "evNewRound", "a", "1=0", "2=0" ); 
} 

public evNewRound( ) { 
    static iButton    = -1; 
    static Float:flGametime = get_gametime(); 
    static Float:flNextthink, Float:flLTime; 
     
    while( ( iButton = find_ent_by_class( iButton, "func_button" ) != 0 ) ) { 
        flNextthink = entity_get_float( iButton, EV_FL_nextthink ); 
        flLTime = entity_get_float( iButton, EV_FL_ltime ); 
         
        if( flNextthink > flLTime && flNextthink > flGametime ) { 
            entity_set_float( iButton, EV_FL_nextthink, flLTime ); 
        } 
    } 
}  
maybe will worK? xD
__________________
wooT now is asking season
will ask you plenty of things for learning

Last edited by --kml--; 05-14-2009 at 10:05.
--kml-- is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-14-2009 , 10:19   Re: while() cpu load 100%
Reply With Quote #3

Quote:
Originally Posted by --kml-- View Post
oo maybe change new to static?

maybe will worK? xD
That is only beneficial if:
  • The function is being called very frequently. Here it is not
  • If the variable requires a lot of memory allocation . Here it does not
  • If you wish the data to remain static [stored in memory between function calls]. Here it is not necessary

Also, FYI, you cannot set a static variables value while declaring.

PHP Code:
//You cannot do:

static iVar 5;

//you CAN do:

static iVariVar 5
__________________
Bugsy is offline
--kml--
Senior Member
Join Date: Jan 2009
Old 05-14-2009 , 10:22   Re: while() cpu load 100%
Reply With Quote #4

ooo i will add that to my referring list thx xD
__________________
wooT now is asking season
will ask you plenty of things for learning
--kml-- is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 05-14-2009 , 10:50   Re: while() cpu load 100%
Reply With Quote #5

@xPaw :

while( ( iButton = find_ent_by_class( iButton, "func_button" ) != 0 ) ) {

->

while( ( iButton = find_ent_by_class( iButton, "func_button" ) ) != 0 ) {
Arkshine is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-15-2009 , 08:27   Re: while() cpu load 100%
Reply With Quote #6

Yes, don't assume =) because this:
PHP Code:
while( ( iButton find_ent_by_classiButton"func_button" ) != ) ) 
it will be ALWAYS true, because what it checks is if iButton = find_ent_by_class() is not 0, and it does not matter what find_ent_by_class() returns because it checks if the assignment worked, and always does, therefore, infinite loop
the above mistake in less code:
PHP Code:
while(var = != 0
the "var = 0" can be anything that returns true, if you want the result of it, you just:
PHP Code:
while((var = 0) != 0
and the loop will stop, because var is 0 and results false for the loop
__________________
Hunter-Digital is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-14-2009 , 11:26   Re: while() cpu load 100%
Reply With Quote #7

@arkshine: o_O i was so lame thanks
__________________
xPaw is offline
--kml--
Senior Member
Join Date: Jan 2009
Old 05-14-2009 , 20:09   Re: while() cpu load 100%
Reply With Quote #8

oo even that mistake could make 100% cpu usage O.O added to my referring list xD
__________________
wooT now is asking season
will ask you plenty of things for learning
--kml-- is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 05-14-2009 , 20:42   Re: while() cpu load 100%
Reply With Quote #9

Quote:
Originally Posted by --kml-- View Post
oo even that mistake could make 100% cpu usage O.O added to my referring list xD
You understood why? It was a infinite loop
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-14-2009 , 22:44   Re: while() cpu load 100%
Reply With Quote #10

Misplaced closing ) I assume
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode