Timer help

I am very new to QNX trying to do a small assignment for school.

I have some code (see below) - probably full of bad methodology

Basically I have two threads one updating values of variables periodically and one outputting variables periodically.

I need to create a timer to use instead of the for loop i used to give me actual seconds in reference to the CPU clock but I have no idea how to do this or where to start. Can someone point me in the right direction?

Here’s my code (please don’t laugh):
#include <pthread.h>
#include
#include

#include <stdio.h>
#include <stdlib.h>
#include <sys/neutrino.h>
#include <sched.h>
#include <unistd.h>

void *DAPsubroutine(void *);
void *DDsubroutine(void *);

#include <time.h>
#include <sys/siginfo.h>

double Ax = 1.0, Ay= -1.0, Az = 10.5,
g=10;
double X=0, Y=0, Z=0, Vx=0, Vy=0, Vz=0;
pthread_mutex_t mutex;

int main(){
std::cout<<“Navigation System\n”;

    for (int T=0; T<21; T++){

    pthread_create(NULL,NULL, &DAPsubroutine,(void *) T);
    pthread_create(NULL,NULL, &DDsubroutine, (void *) T);
    }
    pause();

return EXIT_SUCCESS;
}

void DAPsubroutine(void c){
int t= (int) c;
if (t%1==0){
pthread_mutex_lock(&mutex);
Vx = Vx + Ax
t;
Vy = Vy + Ay
t;
Vz = Vz + (Az-g)*t;

    X = X + Vx*t;
    Y = Y + Vy*t;
    Z = Z + Vz*t;

    pthread_mutex_unlock(&mutex);
    }

}

void *DDsubroutine (void *c){
int t = (int) c;
if (t%5==0){
pthread_mutex_lock(&mutex);
std::cout << "For T = " << t << “seconds\n”;
std::cout << "Vx = " << Vx;
std::cout << "\nVy = " << Vy;
std::cout << "\nVz = " << Vz;
std::cout << "\nX = " << X;
std::cout << "\nY = " << Y;
std::cout << "\nZ = " << Y << “\n”;
pthread_mutex_unlock(&mutex);
}
}

You aren’t creating 2 threads you are creating 42 threads ;-)

The way the mutex and the threads are setup means they will never get to run in parallel.

What for \n in std::cout, unlike printf they will NOT cause the output to be flushed to the console, use std::endl or cout.flush().

I’m not sure what you mean about the timer. If all you need to know is the time then check out time(), clock_gettime() or clock(). For even more precision ClockPeriod()/ClockCycles().

ok - then im doing this all wrong … basically - what I needed to do is create two threads running in parallel, one thread modifying data every 1 second and the other outputting the data every 5 seconds - I was supposed to use Mutex to synchronize the reads and writes to the variables. Basically - i didn’t need the for loop there - i just put it in to give me the timing data i was looking for.

So - I guess what I need to do is have these synch on a clock events, is that right?

I need to compute the execution time for each thread - so i am assuming I can use the ClockCycles() function at the beginning and end of each thread to compute this…

Thanks for the reply.

If the requirement is time based then all you need is the function sleep() there doesn’t seems to be any requirement that each thread must be synchronised.

In your case sleep should be good enough because the work you are doing in the thread is very short.

The BEST way would be to create one timer per thread that would fire at 1 second and 5 seconds and wake the thread up.

PS I’m not giving all the details since this is after all school work right ;-)