Computer scienceProgramming languagesKotlinConcurrency and parallelismConcurrency

Advanced atomic operations

Theory

Safe stock decrement with loop

Report a typo

You're implementing a system that decrements a stock counter, but only if there is stock left.
Use loop to ensure that the operation is retried if the value changes during the update.

Your task is to arrange the following lines to form a correct and safe implementation of this behavior.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                
              
                else return@loop break
              
                stock.loop { current ->
              
                val stock = atomic(10)
              
                }
              
                if (current > 0) return@loop current - 1
              
                fun tryDecrement() {
              
                }
              
___

Create a free account to access the full topic