我需要设置一个定期执行的等待计时器。
捕获是以下内容,周期会改变每个X(比如说5个)周期,具体取决于系统在最后5个周期内执行任务所花费的时间。
我尝试使用自动重置等待计时器,它在每次迭代后被设置(5次中有4次,间隔相同)。然而,时机与我所设定的无关。相关代码如下所示。 (一切都以调用HandleSensor开始)
float AnalysisClient::GetNewInterval()
{
...
return newInterval;
}
VOID CALLBACK AnalysisClient::TimerFinished(LPVOID lpArg,
DWORD dwTimerLowValue,
DWORD dwTimerHighValue)
{
LARGE_INTEGER t;
AnalysisClient* This = (AnalysisClient*)lpArg;
This->readsensor();
t.QuadPart = GetNewInterval() * 10000i64;
SetWaitableTimer(_sensorTimer, &t, 0, TimerFinished, This, TRUE);
}
void AnalysisClient::WaitForsensor()
{
LARGE_INTEGER t;
t.QuadPart = 0;
SetWaitableTimer(_sensorTimer, &t, 0, TimerFinished, this, TRUE);
SleepEx(
INFINITE, // Wait forever.
TRUE);
}
void AnalysisClient::readsensor()
{
EnterCriticalSection(&_sensorCS);
{
while (_numsensorSampled >= _capacity) //no more sensors than the capacity size
{
UtilsLog("Queue full, not doing more sensor readings", UtilsDebug);
SleepConditionVariableCS(&_sensorQueueFullCV, &_sensorCS, INFINITE);
}
UtilsLog("Read sensor", UtilsDebug);
void* sensorreading = _fnSamplesensor();
_numsensorSampled++;
_localsensorQueue.push(sensorreading);
_sensorHandler->Sendreading(sensorreading);
}
WakeConditionVariable(&_sensorQueueEmptyCV);
LeaveCriticalSection(&_sensorCS);
}
void* AnalysisClient::Handlesensor()
{
//Spawn async thread if not already started
if (!_asyncsensorHandlerStarted)
{
_asyncsensorHandlerStarted = true;
CreateUtilsThread(Asyncreadsensor, this);
}
//Free memory from previous invocations
...
void* returnVal = NULL;
EnterCriticalSection(&_sensorCS);
{
while (_localsensorQueue.empty())
SleepConditionVariableCS(&_sensorQueueEmptyCV, &_sensorCS, INFINITE);
_lastreading = _localsensorQueue.front();
_localsensorQueue.pop();
returnVal = _lastreading;
_numsensorProcessed++;
}
LeaveCriticalSection(&_sensorCS);
return returnVal;
}
DWORD WINAPI AnalysisClient::Asyncreadsensor(void* Param)
{
//Create sensor Timer
_sensorTimer = CreateWaitableTimer(NULL, FALSE, NULL);
if (!_sensorTimer)
{
UtilsLog("Unable to create sensor waitable timer", UtilsError);
return 1;
}
AnalysisClient* This = (AnalysisClient*)Param;
This->WaitForsensor();
return 0;
}
bool CreateKahawaiThread(LPTHREAD_START_ROUTINE function, void* instance)
{
DWORD ThreadID;
HANDLE thread = CreateThread(NULL,0,function, instance, 0, &ThreadID);
if(thread==NULL)
return false;
return true;
}
执行日志显示以下执行(右边的数值是将消息写入日志的时间,以毫秒为单位.zi调整了对GetNewInterval的调用以始终返回1600000i64,因此我可以将其作为源的丢弃计时器调用之间的时间仍与计时器调用无关。可以看到的容量设置为5.
30621, Read sensor
30623, Read sensor
30624, Read sensor
30625, Read sensor
30626, Read sensor
30627, Queue full, not doing more sensor readings
30980, Read sensor
30981, Queue full, not doing more sensor readings
30997, Read sensor
30998, Queue full, not doing more sensor readings
31007, Read sensor
31008, Queue full, not doing more sensor readings
31019, Read sensor
31020, Queue full, not doing more sensor readings
31032, Read sensor
31033, Queue full, not doing more sensor readings
31040, Read sensor
31041, Queue full, not doing more sensor readings
31054, Read sensor
31055, Queue full, not doing more sensor readings
31066, Read sensor
31068, Queue full, not doing more sensor readings
31080, Read sensor
31081, Queue full, not doing more sensor readings
31087, Read sensor
31088, Queue full, not doing more sensor readings
31094, Read sensor
31096, Queue full, not doing more sensor readings
31111, Read sensor
31112, Queue full, not doing more sensor readings
31121, Read sensor
31123, Queue full, not doing more sensor readings
31180, Read sensor
31181, Queue full, not doing more sensor readings
31194, Read sensor
31197, Queue full, not doing more sensor readings
31294, Read sensor
31295, Queue full, not doing more sensor readings
31307, Read sensor
31308, Queue full, not doing more sensor readings
31316, Read sensor
31318, Queue full, not doing more sensor readings
31332, Read sensor
31333, Queue full, not doing more sensor readings
31336, Read sensor
31337, Queue full, not doing more sensor readings
31343, Read sensor
31344, Queue full, not doing more sensor readings
31359, Read sensor
31360, Queue full, not doing more sensor readings
31369, Read sensor
31371, Queue full, not doing more sensor readings
31384, Read sensor
31385, Queue full, not doing more sensor readings
31391, Read sensor
31392, Queue full, not doing more sensor readings
31398, Read sensor
31399, Queue full, not doing more sensor readings
错误是什么?是否有更好的方法来实现这一目标?
由于
答案 0 :(得分:1)
你有
while (_numsensorSampled >= _capacity) //no more sensors than the capacity size
和
_localsensorQueue.push(sensorreading);
_numsensorSampled++;
和
_localsensorQueue.pop();
returnVal = _lastreading;
_numsensorProcessed++;
但没有
--_numsensorSampled;
所以
while (_numsensorSampled >= _capacity) //no more sensors than the capacity size
当你达到这个数字时,总是如此。将其更改为
while (_numsensorSampled - numsensorProcessed >= _capacity) //no more sensors than the capacity size
也应该让它运作起来。还
WakeConditionVariable(&_sensorQueueEmptyCV);
LeaveCriticalSection(&_sensorCS);
看起来不对,也许应该是
LeaveCriticalSection(&_sensorCS);
WakeConditionVariable(&_sensorQueueEmptyCV);
否则,当关键部分仍处于活动状态时,唤醒进程可能会再次立即进入睡眠状态。