Hi to all,


I wrote a multithread program and I want to create a timer for each thread to do some set and reset kind of actions in each thread. I cant use global variables and cant use mutex locks also.. because in my application i have to run more than 5000 threads in 8 minutes(this is not a problem now).

i tried a sample code as follows.. I dont kno will it create n number of timer. this code may help u to understand my requirement.

Code:
void udpserver(int);
void logoff_signal(int i)
{
        printf("system is logging off \n");
        udpserver(1000);
}

void hibernate_signal(int i)
{
        printf("System is Hibernating \n");
        udpserver(1000);
}

void idle_starts(int i)
{
        printf("idle time over(system is idle)!!!!!!!!after  sec!!!!!!!!\n");

        struct itimerval logoff, hibernate;

        logoff.it_value.tv_sec = 60;

        hibernate.it_value.tv_sec = 120;

        setitimer(ITIMER_REAL, &logoff, 0);
        signal(SIGALRM, logoff_signal);

        setitimer(ITIMER_REAL, &hibernate, 0);
        signal(SIGALRM, hibernate_signal);
}

int random_generator()
{
        return (rand() % MAX_RAND) + 1;
}

 //thread init function
void *thread_func(void *arg)
{
        int portid = (int)arg;
        char* threadstarttime = timestamp(); //get thread start time

        struct itimerval idle;
        int randomtime = random_generator();
        printf("random time is %d\n", randomtime);
        idle.it_interval.tv_sec=0;
        idle.it_interval.tv_usec = 0;
        idle.it_value.tv_sec = randomtime;
        idle.it_value.tv_usec = 0;

        setitimer(ITIMER_REAL, &idle,0);  //start timer for idle time
        signal(SIGALRM,idle_starts);

}



int main(int argc , char* argv[])
{

        pthread_t tid[atoi(argv[1])];
        int i , j;

        int portnu = atoi(argv[2]);

        if(argc != 3)
        {
                printf("Give number of agents and starting port number as command line arguments\n");

                exit(1);
        }

        //creating threads
        for(i = 0; i < atoi(argv[1]); i++)
        {
                pthread_create(&tid[i], NULL, &thread_func, (void *)portnu+i);
        }

        for(j = 0; j < atoi(argv[1]); j++)
        {
                pthread_join(tid[j], NULL);
        }

return 0;
}

Is this way is better or shall i use creat_timer(). And I want to pass some more arguments like portnu to signal handler idle_starts. How to do achieve all this.. If you have any idea except sleep and wait will be aprreciated. please guide me..


thanks in advance..