Code is working when the number of tasks is a multiple of the number of threads