我有一个运行 while(1) 循环的线程。在那个循环中,我不断检查时间,因为我需要在某些时间执行某些任务。但是,当我将时间打印到屏幕上时,我会看到几秒钟内出现一个几乎 700 毫秒的“空洞”。我尝试设置进程优先级:
policy = SCHED_FIFO;
param.sched_priority = 18;
if( sched_setscheduler( id, policy, ¶m ) == -1 )
{
printf("Error setting scheduler/priority!\n");
}
以及线程优先级:
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
但这没有帮助。
我获得时间的方式是:
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;
或者:
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;
}
现在打印时间:
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;
有任何想法吗?
谢谢!