Using a Queue to average a collection of 9 float values. Once in a while (it usually works!) I am getting the following error
The offending line is the last in this code excerpt
I believe the error is happening because my queue is being modified at the same time as the iteration.
And I need ‘locks’ around the reads and updates. Can anyone help me implement this?
private bool OnPersonUpdated(IEvent evt)
{
Event_Update castEvent = evt as Event_Update;
if (castEvent != null)
{
if (peopleDict.ContainsKey(castEvent.id))
{
float xVel = castEvent.velX;
GameObject cubeToMove = peopleDict[castEvent.id];
if (xVel > 0)
{
float xPos = -1f * positionForPerson(castEvent.person);
float dif = xPos - _prevX;
if (dif < .5f)
{
_posQueue.Enqueue(xPos);
}
if (_posQueue.Count >= 10)
{
_posQueue.Dequeue();
}
float avPos = _posQueue.Average();