Home > database >  Is it good idea to use atomic boolean in kotlin?
Is it good idea to use atomic boolean in kotlin?

Time:02-22

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?

CodePudding user response:

It depends on what you're trying to achieve.

For scenario 2, most of the time what you want is a value that's computed once and reused later. For this use-case, you can use lazy delegates like this:

class MyClass {
    
    // will be computed the first time it's accessed, and then reused
    val value by lazy { computeExpensiveValue() }
}


fun computeExpensiveValue(): Int {
    // some complex stuff
    return 42
}

I never encountered scenario 1, but in cases where lazy is not sufficient you could indeed use atomic booleans for CAS approaches, or locks. Check for instance kotlinx.atomicfu.

If you're using Kotlin coroutines, you might want to check other synchronization primitives like Mutex and Semaphore. But in general it's preferable to make coroutines communicate over channels rather than sharing mutable state.

CodePudding user response:

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()
  }
}
  • Related