write timer class for application threads but its not working

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);
}
 
V

Victor Bazarov

Pallav said:
i am trying to write timer class for application threads but its not
working

Threads can be tough sometimes, can't they? If you have a C++ language
question to ask, we're here for you.
[..OS-specific code snipped..]

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top