using compare and swap to ensure thread safety