question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Should variable in coroutine body be synchronized with AtomicReference or Channel if only one inner coroutine changes it?

See original GitHub issue

Having simple variable in coroutine block and only one producer coroutine changes the state of the variable and another coroutine consumes it, should this variable be synchronized somehow with AtomicReference or Channel to be visible in reader coroutine or reader coroutine automatically fetches new data from the memory?

fun main() = runBlocking<Unit>(Dispatchers.Default) {
    var state: State? = null

    launch {
        data0Flow.collect { newState ->
            state = newState
        }
    }

    launch {
        data1Flow.collect { someData ->
            // Will the state be visible immediately when first coroutine updated it ?
            if (state != null) {
                // do something
            }
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fvascocommented, Jul 22, 2019

should this variable be synchronized somehow

Yes, absolutely.

Local variables are not thread safe.

1reaction
fvascocommented, Jul 23, 2019

read coroutine can cache the state and it will not have fresh values

Yes, on JVM you can get worse than you expect (see JVM Memory Model).

But changing state within the same coroutine (launched by launch, async, flow, …) is safe, correct ?

Yes, this is my experience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared mutable state and concurrency - Kotlin
The main problem being synchronization of access to shared mutable state. Some solutions to this problem in the land of coroutines are ...
Read more >
Coroutines answer to the problem with the mutable state
This solution works, but there are a few problems. The biggest one is, that inside synchronized block you cannot use suspending functions. The...
Read more >
Suspending function can only be called within coroutine body
A Firestore snapshot listener is effectively an asynchronous callback that runs on another thread that has nothing to do with the coroutine ......
Read more >
Shared mutable state with Coroutines | by Nazar Ivanchuk
CoroutineContext represents common pool of shared threads. Body is a suspend function which will be called inside the coroutine.
Read more >
C++ coroutines: Initial implementation pushed to master
C++ coroutines, defined by C++20 standard is nothing but a syntax ... uses of this object should you decide to change the nested...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found