In Scala, if I have a simple class like this:
val calc = actor { var sum = 0 loop { react { case Add(n) => sum += n case RequestSum => sender ! sum } } }
Should my sum field be marked @volatile ? Although the actor is logically single-threaded (i.e. Messages are processed sequentially), individual reactions can occur on separate threads, and therefore, the state variable can be changed in one thread and then read from another.
scala thread-safety volatile actor
oxbow_lakes
source share