Added locking for write() operations.