2

I've started to learn Scala and Akka as the actor model. In an imperative language like C for example i can use several different methods for synchronizing threads on for example a binary tree; a simple semaphore or a mutex, atomic operations, and so on.

Scala however, is a functional object oriented language which can implement an actor model utilizing the Akka library (for example). How should synchronization be implemented in Scala? Let's say that i have i binary tree which my program is suppose to traverse and perform different operations upon. How should i make sure that two different actors isn't, for example, deleting the same node simultaniously?

2
  • 1
    As asked, the answer is Scala is mostly the same as in Java, you use the built-in synchronized HOF. But I suspect the question you mean to ask is "What are the best practices for achieving thread safety for concurrent / parallel programs?" For that, the story is less simple. Actors and Futures go a very long way and each have their strengths and weaknesses and largely complement each other. Commented Apr 2, 2014 at 14:25
  • Thank you for your response @RandallSchulz. I am new to Scala so i don't know so much about the language specific terminology. However i know concurrent programming, and you are right. My question is more of the type, how to achieve thread safety. Commented Apr 3, 2014 at 6:32

2 Answers 2

9

If you want to do synchonized access to data structures, just use the synchronized method of AnyRef to synchronize a block. For example:

object Test {
  private val myMap = collection.mutable.Map.empty[Int, Int]
  def set(key: Int, value: Int): Unit = synchronized { myMap(key) = value }
  def get(key: Int): Option[Int] = synchronized { myMap.get(key) }
}

However, the point of using actors is to avoid threads blocking one another, which would hurt scalability. The Actor way of managing mutable state is for state to be private to Actor instances, and only updated or accessed in response to messages. This is a more complex design, something like:

// define case classes Get, Set, Value here.
class MapHolderActor extends Actor {
  private val myMap = collection.mutable.Map.empty[Int, Int] 
  def receive {
    case Get(key) => sender ! Value(myMap.get(key))
    case Set(key, value) => myMap(key) = value
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

om-nom-nom got in while I was typing! My answer corresponds to om's points 2 and 1, respectively.
5
  1. On a high level you can use Actor as a mutex: it processes all incoming messages one by one.
  2. On the lower levels (but not at the actor level), nothing stops you from using plain old java concurrency primitives
  3. Use immutable data structures, as @Tanmay proposed, so there will be no in-place modification, thus no data races
  4. There are transactors (although deprecated in the very recent akka versions)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.