Questions / Quant development, Concurrency & coordination
Reserve a block of ticket numbers
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
- 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.