Why a thread-local variable in sense reversing barrier (and not simply local)?

138 views
Skip to first unread message

Andrey Brito

unread,
Apr 3, 2013, 9:56:44 PM4/3/13
to art-of-multiproc...@googlegroups.com

Hi,

while looking at the code for the sense reversing barrier, I could not figure out why the code (see below) needs a thread-local variable and not simply have mySense get the negation of the main "sense" variable. I cannot see the need for each thread to keep state between invocations. Did I miss something?


Thank you.

[]'s

Andrey

---------------

public class Barrier {
 AtomicInteger count;
 int size;
 boolean sense = false;
 threadSense = new ThreadLocal<boolean>…

 public void await {
  boolean mySense = threadSense.get(); 
  if (count.getAndDecrement()==1) {
   count.set(size); sense = !mySense
  } else {
   while (sense != mySense) {}
 }
 threadSense.set(!mySense)}}}
Reply all
Reply to author
Forward
0 new messages