Working with parallel threads using semaphores