Jakub Gwóźdź
04/15/2025, 9:26 AMsimon.vergauwen
04/15/2025, 9:41 AMkotlin.concurrent.atomics
package.
It possible due to the function being defined inline
, but it's generally bad practice because slow operations are prone to being conflicting.
The problem with update
& co is that they run in a loop under the hood, so if another operation changes the value the update
function is run again. So if you have a fast operation vs a slow operation than the fast operation will always win, and the slow operation will run in a loop because it's never fast enough to update the value in an atomic way.Jakub Gwóźdź
04/15/2025, 9:42 AMsimon.vergauwen
04/15/2025, 9:47 AMmodify
function that allows you to modify the atomic state and extract information from that operation.
It requires a split between "state management" and executing the required work afterwards. This pattern can be used to safely implement a semaphore, and other complex concurrency types.
https://github.com/arrow-kt/arrow/blob/9579e10672e41d740521e9a30f3d7367a01f430c/ar[…]-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Atomic.ktJakub Gwóźdź
04/15/2025, 9:52 AMprivate val cache = Atomic<State>(State(Instant.now(), some, other, data))
fun refreshIfNeeded() = cache.updateAndGet { prev ->
if (state.timestamp > Instant.now().minusSeconds(5)) prev
else myKtorBasedClient.performSuspendingNetworkFetch()
}
But I decided that it's not work the risk, at worst case scenario I'll get one or two concurrent calls to server, which does not hurt me at all 🙂Charlie Tapping
04/15/2025, 11:14 AM