Using static variable in function vs passing variable from caller
Posted
by Patrick
on Stack Overflow
See other posts from Stack Overflow
or by Patrick
Published on 2010-06-03T10:21:56Z
Indexed on
2010/06/03
10:24 UTC
Read the original article
Hit count: 299
I have a function which spawns various types of threads, one of the thread types needs to be spawned every x seconds. I currently have it like this:
bool isTime( Time t )
{
return t >= now();
}
void spawner()
{
while( 1 )
{
Time t = now();
if( isTime( t ) )//is time is called in more than one place in the real function
{
launchthread()
t = now() + offset;
}
}
}
but I'm thinking of changing it to:
bool isTime()
{
static Time t = now();
if( t >= now() )
{
t = now() + offset;
return true;
}
return false;
}
void spawner()
{
if( isTime() )
launchthread();
}
I think the second way is neater but I generally avoid statics in much the same way I avoid global data; anyone have any thoughts on the different styles?
© Stack Overflow or respective owner