I am writing a program to send pulse outputs at every second, according to my system clock. It's important that this is as accurate as I can make it.
I am using XCode as my debugger at the moment before I independently run this on a different system.
I am using a detached pthread
which sends the output via a (not written by me) function.
I know about the sleep(int seconds)
call, however this is not synced on the clock, and I'm not too sure about the accuracy...
I searched around for a better solution, but have only come across C's time.h
tm
struct. Is there a way to block my thread until the second on a tm
changes, or any other better solutions?
Here is my thread function for reference.
/*One PPS*/
void *_THD_One_PPS(void *args) {
/* Description: Send a 1 millisecond pulse every second */
selfargs_t *self = args;
pthread_detach(pthread_self());
double dutyCycle = 0.001;
long unsigned onTime = dutyCycle * 1000000000; // Time signal high, ns
while (self->myself->ThreadActiveStatus != THD_DONE) {
sleep(1); //TODO: Sync with clock as much as possible
printf("/\\ ");
setPinValue(BBB_GPIO_PIN_ONE_PPS, ON);
pauseNanoSec(onTime);
setPinValue(BBB_GPIO_PIN_ONE_PPS, OFF);
}
printf("Pulse Per Sec DIED");
_T_ThreadsDied++;
_T_ThreadsAlive--;
self->myself->ThreadActiveStatus = THD_DONE;
free(self->arguments);
free(self);
return NULL;
}
Is there an easy or better way to do this?