Fixed deadlock bug, uses atomic.