concurrency - Immutable collection (an example in book of scala in depth) -
in book, has following example, sync insert, didn't sync lookup.i knew currentindex point different object after insert, operation of pointing different object atomic? example, suppose pointer of 8 bytes (on 64 bit machine), @ point currentindex switch pointer, if switch not atomic, such first switch first 4 bytes, , switch second 4 bytes, , if between first , second switch, lookup data, pointer points object never exists , cause problem
class immutableservice[key, value] extends service[key, value] { var currentindex = new immutablehashmap[key,value] def lookup(k: key): option[value] = currentindex.get(k) def insert(k: key, v: value): unit = synchronized { currentindex = currentindex + ((k, v)) } }
yes, currentindex reference - changes done atomically.
check:
Comments
Post a Comment