Saturday, 15 August 2015

concurrency - Using an IndexedSeq of monitors or locks in scala -



concurrency - Using an IndexedSeq of monitors or locks in scala -

i have mutable array have 2 types of calls:

def write(content, index): unit def read(index): content

i want have synchronized block ensures read next write homecoming same contents written:

<something>.synchronized { write(content, index) require(content == read(index)) }

i need have parallelism multiple threads can access different indexes concurrently. obviously, can accomplish same functionality array of locks, using lock , unlock seems overkill since synchronized blocks simpler, , using like:

val monitors = (0 until n).map(i => i.tostring) ... monitors(index).synchronized { write(content, index) require(content == read(index)) }

or even

val monitors = (0 until n).map(_ => new object) ... monitors(index).synchronized { write(content, index) require(content == read(index)) }

seems little hack. there improve way in scala?

the popular way concurrent programming in scala using akka. if need multiple concurrent reads , exclusive writes, simple way accomplish through agents.

in particular case, have array of agents. benefit gain array can immutable, since individual agents take care of changes values.

scala concurrency

No comments:

Post a Comment