using semaphore to synchronize multiple threads