implement locking for multi-thread system