implement the binary semaphore to protect shared memory