Timer issue in multi threading
[R]Hi,
Iam using timers in posix multithreading environment also facing the problem. When ever timer expires control is coming out of the program. Can some one plz help me.
My code is looks like
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void (timer_callback) (union sigval);
/*
* Timer initialization function
*/
int timer_initialize(timer_t * timer_id, int period, timer_callback * cbf, void* data)
{
int status =0;
struct itimerspec ts;
struct sigevent se;
/* register callback func */
se.sigev_notify = SIGEV_THREAD;
se.sigev_value.sival_ptr = data;
se.sigev_notify_function = cbf;
se.sigev_notify_attributes = NULL;
if(timer_create(CLOCK_REALTIME, &se, timer_id) != 0)
{
printf(" error in creating a timer ");
exit(1);
}
/* assign the period */
ts.it_value.tv_sec = period;
ts.it_value.tv_nsec = 0;
if(timer_settime(*timer_id, 0, &ts, 0) != 0)
{
printf(" error in setting timer ");
exit(1);
}
return 0;
}
/*
* scanTimer callback function
*/
void scanTimer_callback(union sigval si)
{
char* evt = (int*) si.sival_ptr;
printf("\n Iam in [%s] callback \n", evt);
}
/*
* tileTimer callback function
*/
void tileTimer_callback(union sigval si)
{
char* evt = (int*) si.sival_ptr;
printf("\n Iam in [%s] callback \n", evt);
}
int main(int argc, char ** argv)
{
timer_t timer_ID;
timer_initialize(&timer_ID, 1, scanTimer_callback, (void *) "Scan" );
timer_initialize(&timer_ID, 2, tileTimer_callback, (void *) "Tile" );
getchar();
return 0;
}[/B]
Last edited by manohar; 12-08-2010 at 11:14 PM.
|