Pergunta de entrevista da empresa J.P. Morgan

2) given a global and a function: int global = 0; void foo() { int local = global;//line 1 local++; //line 2 global = local; //line 3 } we have 10 threads that each of them iterates this function 10 times (no synchronization) what are the largest and smallest possible values of the global?

Resposta da entrevista

Sigiloso

12 de dez. de 2017

2) again, they where very impationt, gave me little time until interrupting me. I think the maximal value is 100 , this happens in case they don't run in parallel but sequential. I think the smallest value is 2 in the following senario: thread 10th runs until line 2; thread 9th runs 9 iterations threads 1-8 all runs until they end, thread 10th complete it's first iteration so now global = 1; thread 9th runs it's last iteration until line 2 so it has local=2 thread 10th complete all of his iterations thread 9th complete it's last iteration and set global=2