Hello. Here's what my drunken neuron has come up with a long time ago.
First declare a structure to gold parameters.
PHP Code:
struct param
{
int(*user_function)(int);
int pass_int;
int interval;
};
Then declare an all-purpose function, which will call your functions.
PHP Code:
void * function(void* _parameter)
{
param * parameter=(param*)_parameter;
if(parameter->user_function)
{
for(;;)
{
(parameter->user_function)(parameter->pass_int);
sleep(parameter->interval);
}
}
}
Declare some dumb class
PHP Code:
class timer
{
param parameter;
pthread_t thread_id;
public:
timer(int _interval,int(*_function)(int),int _pass_int)
{
parameter.interval=_interval;
parameter.user_function=_function;
parameter.pass_int;
pthread_create(&thread_id,NULL,&function,¶meter);
}
};
Two "user" functions
PHP Code:
int function_to_run(int a)
{
cout<<"bla"<<endl;
}
int function_to_run2(int a)
{
cout<<"fdgdfg"<<endl;
}
Cross your fingers..
PHP Code:
int main(int argc, char** argv)
{
timer *tm2=new timer(2,&function_to_run2,43);
timer *tm=new timer(1,&function_to_run,47);
while(true);
return 0;
}
Of course, I have done no error testing, and as you can see, I am really not using the int parameter to the functions, nor am I using the functions return values.. (ugh.. the values they should return
).
Anyways, I am sure there's lots and lots of timer classes on the net.