Questions / Quant development, Concurrency & coordination

Reserve a block of ticket numbers

Foundation, about 6 minutes. Question D201.

The two atomic operations execute in the displayed order. What ticket does worker B receive?

atomic<int> next{40};
int a = next.fetch_add(3);
int b = next.fetch_add(2);

Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.

Show the answer and worked solution

Answer: 43 ticket

  1. A receives 40 and reserves 40, 41, and 42. B therefore receives 43, and the next unused ticket becomes 45.

Interview shortcut. fetch_add returns the old value: A gets 40–42, B gets 43.

In the app this has a follow-up where one thing about the setup changes. Try the follow-up.

More concurrency & coordination questions