P
Pallav singh
Hi All,
i am trying to write timer class for application threads but its not
working
Thanks
Pallav singh
++++++++++++++++++++++++++++++++++++++++++++++
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_THREADS 5
pthread_t threads[NUM_THREADS];
static int count = 0;
void SimpleTimer(int timeinterval)
{
int starttime, currenttime, difference;
starttime = time(NULL);
count++;
printf("called for %d times \n\n", count);
do
{
currenttime = time( NULL);
difference = currenttime - starttime;
}
while (difference < timeinterval);
}
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
if( tid == threads[2] )
{
printf("Thread 2 calls the timer \n\n");
SimpleTimer( 3 );
sched_yield();
printf("Thread 2 sending cancel to Thread 3 \n\n");
pthread_cancel(threads[3]);
}
if( tid == threads[1] )
{
printf("Thread 1 going to sleep and calls the timer \n\n");
SimpleTimer( 13 );
sched_yield();
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n",
rc);
return 0;
}
}
pthread_exit(NULL);
}
i am trying to write timer class for application threads but its not
working
Thanks
Pallav singh
++++++++++++++++++++++++++++++++++++++++++++++
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_THREADS 5
pthread_t threads[NUM_THREADS];
static int count = 0;
void SimpleTimer(int timeinterval)
{
int starttime, currenttime, difference;
starttime = time(NULL);
count++;
printf("called for %d times \n\n", count);
do
{
currenttime = time( NULL);
difference = currenttime - starttime;
}
while (difference < timeinterval);
}
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
if( tid == threads[2] )
{
printf("Thread 2 calls the timer \n\n");
SimpleTimer( 3 );
sched_yield();
printf("Thread 2 sending cancel to Thread 3 \n\n");
pthread_cancel(threads[3]);
}
if( tid == threads[1] )
{
printf("Thread 1 going to sleep and calls the timer \n\n");
SimpleTimer( 13 );
sched_yield();
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n",
rc);
return 0;
}
}
pthread_exit(NULL);
}