is there a concurrent list/queue that can atomical...
# announcements
n
is there a concurrent list/queue that can atomically return its content and clear itself afterwards ?
a
This should work, but might be not efficient:
atomic<List<T>>(emptyList())
Using e.g. atomicfu lib
b
I've used
AtomicReference
for this use case. Something like:
Copy code
private val myList = AtomicReference<MutableList<String>>(mutableListOf())

fun addValue(value: String) {
    myList.get().add(value)
}

fun getAndClear(): List<String> {
    // Return the current list, and re-set it to an empty one
    return myList.getAndSet(mutableListOf())
}
(which may be the same thing @Arkadii Ivanov was suggesting, I'm not familiar with atomicfu)
a
Exactly, atomicfu is a multiplatform implementation
👍 1