title: Visibility in Kotlin coroutines
body: <p>I decided to dive in into Kotlin coroutines. I have some questions regarding visibility. I understand that in absence of a <code>ContinuationInterceptor</code>, it is possible different sections of the same coroutines to be executed by different threads.</p>
  
  <p>How is guaranteed that, after a suspension, the new thread has the correct visibility of the coroutine internal state ?</p>
  
  <p>For example:</p>
  
  <pre><code>    suspend fun doPost(customRatings : Map&lt;String, Int&gt;) : Int {...}
  
      fun postRatings_1() {
          GlobalScope.launch {
              val mutableMap : MutableMap&lt;String, Int&gt; = mutableMapOf()
  
              mutableMap["Apple"] = 1
              mutableMap["Banana"] = 2
  
              // ...
  
              val postId = doPost(mutableMap)
  
              // How am I guaranteed to see the two entries here ?
              println("posted ${mutableMap} with id ${postId}") 
          }
      }
  </code></pre>
  
  <p>Similarly when a new coroutine is launched</p>
  
  <pre><code>    fun postRatings_2() {
  
          val mutableMap : MutableMap&lt;String, Int&gt; = mutableMapOf()
  
          mutableMap["Apple"] = 1
          mutableMap["Banana"] = 2
  
  
          GlobalScope.launch {
              // How am I guaranteed to see the two entries here ?
              val postId = doPost(mutableMap)
              //...
          }
  </code></pre>
  
  <p>In both of these cases there is some mutable state shared between two (existing) threads.</p>
  
  <p>I am looking for the "Happens-before" rule that guarantees that the mutable state will be properly visible by the two threads.</p>