switch to using atomic variables and spin locking, as you cannot lock and unlock in different threads (causes crash in cygwin)