typelevel / typelevel/cats-effect

Document that Fibers can close over non-volatile vars

Open
#2,484 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

:books: docs
Dominant language
Scala
Stars
2.2k
Forks
576
Avg merge
2d 11h
Merged PRs (30d)
18

Description

a fiber can close over a non volatile var safely
we should add it to the docs because it's come up a few times

☝️

The (hacky, non-FP) example I used to ask about this:

  def actorThatOnlyUpdatesState[F[_]: Async, S](
      initialState: F[S]
  ) = for {
    inbox <- Queue.unbounded[F, S => S]
    s <- initialState
    _ <- {
      var state = s
      inbox.take.map(f => state = f(state)).foreverM
    }.start
  } yield inbox.offer _

This loops over a Queue that contains S => S, which updates the state. I can then push S => S functions onto that queue, which will then be executed. Apparently the state = f(state) is not an issue here from a thread-safety perspective.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Scala fiber and Queue example included in the issue, then locate the existing documentation covering fibers and concurrency. Document when a fiber can safely close over a non-volatile variable, using the example to address the thread-safety question; done means the guidance is clear to readers facing the same pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.