I would solve this problem NOT with some waiting function, but a semaphore solution. Semaphore are resources which allowing some designated access and so fitting best for your probelm.
For learning on semaphores is a short explanation from
Microsoft and also some
deeper insights.
This link may help you in finding the final solution on
Stack overflow.