With semaphores and the loops inside the functions