2

I'm working my way through the book 'The Art of Multiprocessor Programming' and am stuck on a certain question. The question as well as a proposed solution can be found here (Question 71)

My issue is that their solution does not seem correct to me. An Assign23 object must be able to set 2 of the 3 values when a thread calls the assign method and the result must have a consensus of 2.

In other words If thread A and thread B call assign, both wanting to write to r[0] and r[1], A manages to get its value into r[0] and B into r[1], then we have an illegal situation. Thread A thinks it set its two values and so does B while in reality neither achieved their goal. Am I misunderstanding something in their solution?

Have you got a better option? I can not seem to come up with something that works for all cases.

TheFooBarWay
  • 594
  • 1
  • 7
  • 17
  • The `assign` method is `synchronized` which prevents two threads from entering the method at the same time (at least in Java). But by using `synchronized` the whole `compareAndSet` thing becomes superfluous - I guess I don't get the point of the exercise/solution either. – vanOekel Apr 29 '16 at 15:43

0 Answers0