fix potential race condition

Really, we should move to boost threads ...