I have a thread which runs a while(1) loop. During that loop I keep checking the time as I need to perform certain tasks on certain times. However, when I print the time to the screen I see that once in a few seconds I get a "hole" of almost 700ms. I tried setting the prcoess priority:
policy = SCHED_FIFO;
param.sched_priority = 18;
if( sched_setscheduler( id, policy, ¶m ) == -1 )
{
printf("Error setting scheduler/priority!\n");
}
as well as the thread priority:
pthread_attr_t attr;
struct sched_param param;
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
param.sched_priority = 50;
pthread_attr_setschedparam(&attr, ¶m);
m_INVThreadID = pthread_create( &m_BaseStationLocatorsThread, &attr,
ThreadBaseStationLocatorsHandler, (void*)
(this));//Linux
But it didn't help.
The way I get the time is either with:
struct timespec start;
clock_gettime( CLOCK_MONOTONIC_RAW, &start);
//gettimeofday(&tim, NULL);
//wInitTime = tim.tv_sec*1000 + tim.tv_usec/1000.0;
double x = start.tv_sec;
double y = start.tv_nsec;
x=x*1000;
y = y/1000000;
double result = x+ y;
return result;
Or:
STime TimeHandler::GetTime()
{
STime tmpt;
time_t rawtime;
tm * timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
tmpt.day_of_month = timeinfo->tm_mday;
tmpt.month = timeinfo->tm_mon+1;
tmpt.year = timeinfo->tm_year+1900;
tmpt.Hours = timeinfo->tm_hour;
tmpt.Min = timeinfo->tm_min;
tmpt.Sec = timeinfo->tm_sec;
tmpt.MilliSeconds = GetCurrentTimeMilliSeconds();
return tmpt;
}
and now print the time with:
STime timeinfo = GetTime();
string curTime;
int datePart;
string datePartSTR;
std::ostringstream convert;
datePart =timeinfo.day_of_month;
convert << datePart;
//curTime.append( convert.str());
convert << "/";
datePart = timeinfo.month;
convert << datePart;
//curTime.append( convert.str());
convert << "/";
datePart =timeinfo.year;
convert << datePart;
//curTime.append( convert.str());
convert << " ";
datePart =timeinfo.Hours;
if (timeinfo.Hours<10)
convert <<0;
convert << datePart;
//curTime.append( convert.str());
convert << ":";
datePart =timeinfo.Min;
if (timeinfo.Min<10)
convert <<0;
convert << datePart;
//curTime.append( convert.str());
convert << ":";
datePart =timeinfo.Sec;
if (timeinfo.Sec<10)
convert <<0;
convert << datePart;
convert << ":";
datePart =timeinfo.MilliSeconds;
if (timeinfo.MilliSeconds<100)
convert << 0;
if (timeinfo.MilliSeconds<10)
convert << 0;
convert << datePart;
curTime.append( convert.str());
return curTime;
Any ideas?
Thanks!