RACE CONDITION.  This is a _strong_ argument for moving the downsampling into this part of the code.  This absolutely does not do what you say it does because you aren't protecting this properly.  Use a semaphore if you have to do it this way.  Or just don't do the check if you aren't that concerned.