我有两个线程;一个呼叫Push()
,另一个呼叫Pop()
。我遇到的问题是,以快速速率(例如100fps)调用Push()
时,Pop()
跟不上速度,程序崩溃了。在较低的帧速率(例如10fps)下没有问题。我可以做些什么来优化这段代码吗?
void AcqCache::Push(FrameData& frameData)
{
// If cache not idle - wait
Monitor::Enter(m_cacheLock);
try
{
m_frameInfoBuffer[(frameData.GetFrameInfo().FrameNr - 1) % m_acqBuffer->BufferSize] = frameData;
// Unlock processing thread
m_frameDataCache->Push(frameData);
Monitor::Pulse(m_cacheLock);
}
finally
{
Monitor::Exit(m_cacheLock);
}
}
void AcqCache::Pop()
{
FrameData frData;
while (m_processFrameRunning)
{
// If cache not idle - wait
Monitor::Enter(m_cacheLock);
try
{
Monitor::Wait(m_cacheLock);
while (!m_frameDataCache->IsEmpty())
{
if (!m_processFrameRunning)
break;
frData = std::move(m_frameDataCache->Pop());
m_acqBuffer->CacheUsage = (int)m_frameDataCache->Count();
// Add frame to frame stack
m_acqBuffer->AddFrame(frData);
}
}
finally
{
Monitor::Exit(m_cacheLock);
}
}
}
谢谢!