Use a lock to synchronize access between threads