use semaphore to write atomically.