Added fix for potential thread bug

A variable is changed without proper mutex locking.