arrow-fx / arrow.fx.typeclasses / Concurrent / MVar

MVar

open fun <A> MVar(a: A): Kind<F, MVar<F, A>>

Create a MVar or mutable variable structure to be used for thread-safe sharing, initialized to a value a.

import arrow.Kind
import arrow.core.Option
import arrow.core.Tuple3
import arrow.fx.*
import arrow.fx.extensions.io.concurrent.concurrent
import arrow.fx.typeclasses.Concurrent

fun main(args: Array<String>) {
  fun <F> Concurrent<F>.mvarExample(): Kind<F, Tuple3<Int, Option<Int>, Int>> =
  //sampleStart
    fx.concurrent {
      val mvar = !MVar(4)
      val four = !mvar.take()
      val empty = !mvar.tryTake()
      val (join, _) = !mvar.take().fork()
      !mvar.put(10)
      Tuple3(four, empty, !join)
    }

  //sampleEnd
  IO.concurrent().mvarExample()
    .fix().unsafeRunSync().let(::println)
}

See Also

MVar

open fun <A> MVar(): Kind<F, MVar<F, A>>

Create an empty MVar or mutable variable structure to be used for thread-safe sharing.

See Also

MVar

MVar

Do you like Arrow?

Arrow Org
<