memory - Difference between scala concurrent and non concurrent class under parallelsim -
in scala - functional programming course on coursera, following snippet described fail in concurrent case. snippet succeed if mutable.set
replaced concurrent class.
def intersection(a: genset[int], b: genset[int]): set[int] = { val result = mutable.set[int]() (x <- a) if (b contains x) result += x result } intersection((0 until 1000).toset, (0 until 1000 4).toset) intersection((0 until 1000).par.toset, (0 until 1000 4).par.toset)
rule: avoid mutations same memory locations without proper synchronization.
what difference between concurrent , non-concurrent class, or reason non-concurrent class can fail under parallelism?
when concurrently append element set without synchronization, set can't handle collisions correctly or calculate position wrongly. example, res2
maybe have duplicate fields or less fields.
explantation:
for:
for (x <- a) if (b contains x) result += x result }
there race condition result += x
. equals result.addentry(x), method it's not thread safe,
var h = index(newentry.hashcode) var curentry = table(h) while (null != curentry) { if (curentry == newentry) return false h = (h + 1) % table.length curentry = table(h) //statistics.collisions += 1 } table(h) = newentry
in above code, when try concurrently append element hashtable. it's maybe caused calculate wrong position or meet wrong collisions. example, when try add newentry
set
, it's not exist in set, directly go table(h) = newentry
, @ same time, there new value, has same hashcode
newentry
, first newentry
still not finish table(h) = newentry
, newentry
override second value.
so synchronization maybe can like:
for (x <- a) { if (b contains x) { this.synchronized { result += x } } }
Comments
Post a Comment