Use semaphore to fix this problem.
Please attached code.