Using static variable in function vs passing variable from caller
- by Patrick
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?