Only write to locked_ while we hold the lock

To avoid race conditions, locked_ should be set to false before we release the lock.