is there a concurrent list/queue that can atomically return its content and clear itself afterwards ?
a
Arkadii Ivanov
12/10/2020, 11:36 AM
This should work, but might be not efficient:
atomic<List<T>>(emptyList())
Arkadii Ivanov
12/10/2020, 11:37 AM
Using e.g. atomicfu lib
b
bbaldino
12/10/2020, 5:45 PM
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())
}
bbaldino
12/10/2020, 5:46 PM
(which may be the same thing @Arkadii Ivanov was suggesting, I'm not familiar with atomicfu)
a
Arkadii Ivanov
12/10/2020, 5:48 PM
Exactly, atomicfu is a multiplatform implementation