Hey I am learning in atomic in kotlin. I am wondering is this good idea to use atomic boolean in my scenario? Can someone suggest, how to do in atomic way.
Scenario 1 Not for first time call
var isFirstTime = true
fun notForFirstTime(){
if(!isFirstTime){
jobDone()
}
isFirstTime = false
}
Scenario 2 only for first time
var isFirstTime = true
fun onlyForFirstTime(){
if(isFirstTime){
jobDone()
}
isFirstTime = false
}
Can I do this in atomic way? Also is this good idea?
It is not very clear what do you ask. But assuming you want to make sure some code is executed (or not executed) only for the first time and the function may be invoked concurrently (because you asked for "atomic") then the answer is: no and no. Both your code samples aren't thread-safe and may result in more than the first call to be handled in a special way.
This is not because boolean is not atomic, but rather because your code is not atomic. Two or more concurrent threads may check for isFirstTime
at the same time, before other thread will flip it to false
.
You can solve this in multiple ways, for example using mutexes, but probably the easiest and most performant is to use one of compare and swap operations. For example:
val isFirstTime = AtomicBoolean(true)
fun notForFirstTime(){
if (!isFirstTime.getAndSet(false)) {
jobDone()
}
}
fun onlyForFirstTime(){
if (isFirstTime.getAndSet(false)) {
jobDone()
}
}