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()
{
Time t = now();
while( 1 )
{
if( isTime( t ) )//is time is called in more than one place in the real function
{
//launchthread and recalculation of t only happens once in 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()
{
while( 1 )
{
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?