Add some locks to protect a variable

None of these changes should really matter, but wrap some reads
of the agent thread count with mutex to prevent helgrind reporting
race conditions.
Prevent duplicate static variable set

This fixes helgrind warning
More around some pthread locks to helgrind

These changes do not effect operation, but eliminate some false
positive errors from helgrind, namely testing some variables values
that are sometimes protected by locks and other times not protected.